[patch] proposed fix for libstdc++/59829
Marc Glisse
marc.glisse@inria.fr
Mon Jan 27 20:12:00 GMT 2014
On Mon, 27 Jan 2014, Jonathan Wakely 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.
For comparison, libc++ has 2 paths. If pointer really is a pointer, it
just returns it, no need to pay for a comparison in that case. And
otherwise, it calls _M_start.operator-> and crosses its fingers. There is
a helper function doing that used throughout the library.
--
Marc Glisse
More information about the Libstdc++
mailing list