On 28 August 2012 16:15, Michael Haubenwallner wrote: > Hi, > > in some old, large, originally C-written application (using gcc-4.2.4 still) > I did have to find a bug that boils down to something like this: > > std::string x; > strcpy( (char*) x.c_str(), "abc"); > > Any subsequent empty std::string instance did contain "abc" instead of "", > which was the issue showing up to the user. > > My idea what could have helped out here was to make the empty string _Rep > object readonly (ie. const), to get a segmentation fault along the strcpy.
Does it actually produce a segfault? I suppose it might on some platforms, but not all, so I'm not sure it's worth changing. (It seems easier to simply track down and shoot the programmer who thought it was OK to cast away const on the string! ;-)