Bug 61486 - std::string not thread safe
Summary: std::string not thread safe
Keywords:
Status: CLOSED NEXTRELEASE
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: libstdc++
Version: 7.3
Hardware: i386
OS: Linux
medium
high
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2002-03-20 15:04 UTC by André Johansen
Modified: 2007-04-18 16:41 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2004-10-01 23:54:05 UTC
Embargoed:


Attachments (Terms of Use)
Test program (882 bytes, text/plain)
2002-03-20 15:06 UTC, André Johansen
no flags Details
Patch adding locking for i386 machines (857 bytes, patch)
2002-03-20 15:09 UTC, André Johansen
no flags Details | Diff

Description André Johansen 2002-03-20 15:04:48 UTC
The std::string implementation that comes with libstdc++ 2.96-98 is not
thread safe.  The problem will result in a corrupted memory heap.

The reason for the problem is improper locking of the reference count in
std::basic_string::Rep.

The problem is also in the version that comes with GCC v2.95.3, but seems to
be fixed in GCC v3.0.4.  If STLport is used as the standard library, the
problem disappears.

I have a test-program that will trigger this:
[andrej@hermes tests]$ g++ -Wall -O3 -pthread string_mt.cpp -lstdc++ -lpthread
[andrej@hermes tests]$ ./a.out
Thread started (1026)
Thread started (2051)
Thread started (3076)
Segmentation fault

Comment 1 André Johansen 2002-03-20 15:06:51 UTC
Created attachment 49145 [details]
Test program

Comment 2 André Johansen 2002-03-20 15:09:06 UTC
Created attachment 49146 [details]
Patch adding locking for i386 machines

Comment 3 André Johansen 2002-03-26 10:15:07 UTC
Notice that all threaded applications, and all libraries using std::string
that can be used in threaded applications must be recompiled.



Comment 4 Need Real Name 2002-07-29 21:07:17 UTC
To restate the severity of this bug -- std::string is worse than just not 
threadsafe, it is completely unsuitable for any program or library compiled for 
multithreading and run on an SMP iA32 system. You can crash in nilRep() by just 
creating empty strings on the stack, as the static nil representation will have 
its reference count go to zero and be freed. 

Calling a class threadsafe typically implies that the same instance can be 
operated upon by different threads. The current implementation of std::string 
does not allow different instances to be operated on by different threads.

Comment 5 André Johansen 2002-11-06 10:14:39 UTC
This problem is still present in Red Hat Linux v7.3. 
gcc-2.96-112 
gcc-c++-2.96-112 
libstdc++-2.96-112 
libstdc++-devel-2.96-112 


Comment 6 Benjamin Kosnik 2004-10-01 23:54:05 UTC
Fixed gcc-3.2.x, gcc-3.3.x, gcc-3.4.x.


Note You need to log in before you can comment on or make changes to this bug.