[libc++] [PATCH] global object initialization bug

WenHan Gu (谷汶翰) wenhan.gu at gmail.com
Mon Mar 4 23:35:38 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/ae9c385e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libcxx.patch
Type: application/octet-stream
Size: 739 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130305/ae9c385e/attachment.obj>


More information about the cfe-commits mailing list