<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:14px">Hi all,</span><div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">If I use cerr/cout... before main function, it may potentially cause use-before-init before this patch.</div>

<div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">This is a tiny patch, but important to guarantee init-before-use.</div><div style="font-family:arial,sans-serif;font-size:14px">

Please review it. Thanks!!</div><div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">

===</div><div style="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">Long explanation:</div><div style="font-family:arial,sans-serif;font-size:14px"><br></div>

<div style="font-family:arial,sans-serif;font-size:14px"><p style="padding-top:0.5em;margin-bottom:0px;font-family:'Arial Unicode MS',Arial,sans-serif;margin-top:0px;padding-bottom:0.5em">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="padding-top:0.5em;margin-bottom:0px;font-family:'Arial Unicode MS',Arial,sans-serif;margin-top:0px;padding-bottom:0.5em">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="padding-top:0.5em;margin-bottom:0px;font-family:'Arial Unicode MS',Arial,sans-serif;margin-top:0px;padding-bottom:0.5em">In GNU libstdc++, it is at </p><p style="padding-top:0.5em;margin-bottom:0px;font-family:'Arial Unicode MS',Arial,sans-serif;margin-top:0px;padding-bottom:0.5em">

<libstdc++>/include/std/iostream: static ios_base::Init __ioinit;</p><p style="padding-top:0.5em;margin-bottom:0px;font-family:'Arial Unicode MS',Arial,sans-serif;margin-top:0px;padding-bottom:0.5em">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="font-family:arial,sans-serif;font-size:14px"><br></div><div style="font-family:arial,sans-serif;font-size:14px">Thanks!</div><div><br></div>-- <br><div dir="ltr">Best Regards,<div>WenHan Gu (Nowar)</div>

</div>
</div>