LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 43300 - constructing an ios_base::Init object clobbers stream state
Summary: constructing an ios_base::Init object clobbers stream state
Status: RESOLVED FIXED
Alias: None
Product: libc++
Classification: Unclassified
Component: All Bugs (show other bugs)
Version: unspecified
Hardware: PC Linux
: P enhancement
Assignee: Marshall Clow (home)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-12 14:18 PDT by Richard Smith
Modified: 2019-09-13 08:28 PDT (History)
2 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Smith 2019-09-12 14:18:22 PDT
Constructing an object of type std::ios_base::Init is supposed to have no effect when the IO streams have already been initialized. In libc++, that is not the case.

Testcase:


#include <iostream>

int main() {
    std::cout << "hello, " << std::boolalpha;
    std::ios_base::Init init_streams;
    std::cout << true << " world!";
}


... should print "hello true world!" but with libc++ prints "hello 1 world!".

This also presumably means that a multithreaded program that constructs an Init object has a data race. This seems straightforward to fix by adding a static one-time initialization guard to the iso_base::Init constructor.

It might also be reasonable to maintain an atomic count of the number of extant Init objects so that the flushes in the destructor are only run when the last one is destroyed, as [ios.init]/4 requires. I think that the current destructor behavior might even result in observable nonconformance in programs that call cout.rdbuf(stream) and observe when stream sees writes.
Comment 1 Marshall Clow (home) 2019-09-12 15:13:12 PDT
Yeah, we don't actually check to see if `Init` has been called before.
Apparently no one does that.
Comment 2 Marshall Clow (home) 2019-09-13 08:28:26 PDT
Fixed in revision 371864.