[PATCH] global object initialization bug
WenHan Gu (谷汶翰)
wenhan.gu at gmail.com
Mon Mar 4 23:31:59 PST 2013
Hi all,
If I use cerr/cout... before main function, it may potentially cause
use-before-init before this patch.
This is a tiny patch, but important to guarantee init-before-use.
Please review it. Thanks!!
===
Long explanation:
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.
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.
In GNU libstdc++, it is at
<libstdc++>/include/std/iostream: static ios_base::Init __ioinit;
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.
Thanks!
--
Best Regards,
WenHan Gu (Nowar)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130305/cf3efaec/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libcxx.patch
Type: application/octet-stream
Size: 741 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130305/cf3efaec/attachment.obj>
More information about the cfe-commits
mailing list