https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91805

            Bug ID: 91805
           Summary: Data race in std::locale::classic
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chris at clearwater dot dev
  Target Milestone: ---

Created attachment 46894
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46894&action=edit
Fixes data race

std::locale::classic() uses gthread_once to initialize the classic locale impl
in a thread-safe manner but then uses placement new without any synchronization
to construct a locale into static global aligned storage.

This is a data race as you can end up with multiple threads all racing to write
to the same memory location.

I suggest the attached fix which moves the placement new into the ghtread_once
block and then simply reinterpret_casts the global static aligned storage in
std::locale::classic(). This should be fine as the constructor is just storing
a pointer the the impl and the destructor is never called.

Reply via email to