This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [patch] proposed fix for libstdc++/59829
- From: Paolo Carlini <paolo dot carlini at oracle dot com>
- To: Jonathan Wakely <jwakely at redhat dot com>
- Cc: "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>, "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 27 Jan 2014 21:17:28 +0100
- Subject: Re: [patch] proposed fix for libstdc++/59829
- Authentication-results: sourceware.org; auth=none
- References: <20140127193851 dot GA16809 at redhat dot com>
Hi,
> On 27/gen/2014, at 20:38, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> This is the best I've come up with to avoid dereferencing an invalid
> pointer when calling vector::data() on an empty vector.
>
> For C++03 we reurn the vector's pointer type, so can just return the
> internal pointer, but for C++11 we need to convert that to a raw
> pointer, which we do by dereferencing, so we must check if it's valid
> first.
>
> Any better suggestions before I commit this?
I'm going to say something quite obvious - in principle the C++11 version could become identical at compile time to the simpler C++03 version depending on the types. Not sure if it's worth it, avoiding the conditional operator at run-time... Maybe libc++ does something similar, if I remember correctly Howard's distaste for branches ;)
Paolo