<div dir="ltr">Hi all,<div><br></div><div>If I use cerr/cout... before main function, it may potentially cause use-before-init before this patch.</div><div><br></div><div style>This is a tiny patch, but important to guarantee init-before-use.</div>

<div style>Please review it. Thanks!!</div><div style><br></div><div style><br></div><div style>===</div><div style><br></div><div style>Long explanation:</div><div style><br></div><div style><p style="margin-top:0px;margin-bottom:0px;padding-top:0.5em;padding-bottom:0.5em;color:rgb(0,0,0);font-family:'Arial Unicode MS',Arial,sans-serif">

std::cout, cerr, ... are global objects that should be initialized before use. However, C++ does not guarantee an order of initialization between static objects in different translation units.</p><p style="margin-top:0px;margin-bottom:0px;padding-top:0.5em;padding-bottom:0.5em;color:rgb(0,0,0);font-family:'Arial Unicode MS',Arial,sans-serif">

One basic idiom is that declares a static object that gets created in every translation unit that includes <iostream>. This object has a static constructor and destructor that initializes and destroys the global iostream objects before they could possibly be used in the file.</p>

<p style="margin-top:0px;margin-bottom:0px;padding-top:0.5em;padding-bottom:0.5em;color:rgb(0,0,0);font-family:'Arial Unicode MS',Arial,sans-serif">In GNU libstdc++, it is at </p><p style="margin-top:0px;margin-bottom:0px;padding-top:0.5em;padding-bottom:0.5em;color:rgb(0,0,0);font-family:'Arial Unicode MS',Arial,sans-serif">

<libstdc++>/include/std/iostream: static ios_base::Init __ioinit;</p><p style="margin-top:0px;margin-bottom:0px;padding-top:0.5em;padding-bottom:0.5em;color:rgb(0,0,0);font-family:'Arial Unicode MS',Arial,sans-serif">

<br></p><p style="margin-top:0px;margin-bottom:0px;padding-top:0.5em;padding-bottom:0.5em;color:rgb(0,0,0);font-family:'Arial Unicode MS',Arial,sans-serif">but in libcxx, it is at src/iostream.cpp, i.e., it only guarantee initialized when entering main function. If we use them before main, it may cause undefined behavior since it is a use-before-init bug.</p>

</div><div style><br></div><div style>Thanks!</div><div style><br></div><div style><br></div><div><br clear="all"><div><br></div>-- <br><div dir="ltr">Best Regards,<div>WenHan Gu (Nowar)</div></div>
</div></div>