This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH][RFC] Avoid indexing std::vector past the end


On 2017-12-28 11:01, Ruslan Kabatsayev wrote:
Hello all,

On my system I have added some asserts into GCC's stl_vector.h, which check for various mistakes like out of bounds access, call to std::vector::front on empty
vector etc. to debug my own projects. After I built GDB with such
modifications, I've noticed that in some cases it accesses some vectors out of bound, namely element one past the end. Effectively the code is something like `auto*p=&someVector[someVector.size()];`, which, although may seem legitimate on the first glance since it simply takes address, is still Undefined Behavior
according to the C++ Standard (see e.g. [1] and links in that page).

So I wonder whether GDB deliberately exploits undefined behavior here knowing that GCC might give(?) some guarantee that this will always work as intended,
or it's simply a mistake, and my patch would be OK.

[1]: https://stackoverflow.com/a/27069592/673852

Regards,
Ruslan

Hi Ruslan,

Thanks for finding and reporting this. We certainly don't want to rely on any compiler-specific undefined behavior, this is a mistake.

The patch looks good to me, it's just missing a ChangeLog entry.

Simon


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]