This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH, libstdc++] Make empty std::string storage readonly
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Michael Haubenwallner <michael dot haubenwallner at salomon dot at>
- Cc: gcc-patches at gcc dot gnu dot org, libstdc++ at gcc dot gnu dot org
- Date: Tue, 28 Aug 2012 17:46:58 +0100
- Subject: Re: [PATCH, libstdc++] Make empty std::string storage readonly
- References: <503CE088.70208@salomon.at>
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! ;-)