public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Ruslan Kabatsayev <b7.10110111@gmail.com>
To: gdb-patches@sourceware.org
Cc: Ruslan Kabatsayev <b7.10110111@gmail.com>
Subject: [PATCH][RFC] Avoid indexing std::vector past the end
Date: Thu, 28 Dec 2017 16:01:00 -0000	[thread overview]
Message-ID: <1514476903-5206-1-git-send-email-b7.10110111@gmail.com> (raw)

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

---
 gdb/psymtab.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index c87ef25..c622f4c 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -1337,21 +1337,21 @@ recursively_search_psymtabs
     }
 
   partial_symbol **gbound
-    = &objfile->global_psymbols[ps->globals_offset + ps->n_global_syms];
+    = objfile->global_psymbols.data() + ps->globals_offset + ps->n_global_syms;
   partial_symbol **sbound
-    = &objfile->static_psymbols[ps->statics_offset + ps->n_static_syms];
+    = objfile->static_psymbols.data() + ps->statics_offset + ps->n_static_syms;
   partial_symbol **bound = gbound;
 
   /* Go through all of the symbols stored in a partial
      symtab in one loop.  */
-  partial_symbol **psym = &objfile->global_psymbols[ps->globals_offset];
+  partial_symbol **psym = objfile->global_psymbols.data() + ps->globals_offset;
   while (keep_going)
     {
       if (psym >= bound)
 	{
 	  if (bound == gbound && ps->n_static_syms != 0)
 	    {
-	      psym = &objfile->static_psymbols[ps->statics_offset];
+	      psym = objfile->static_psymbols.data() + ps->statics_offset;
 	      bound = sbound;
 	    }
 	  else
-- 
1.9.1

             reply	other threads:[~2017-12-28 16:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-28 16:01 Ruslan Kabatsayev [this message]
2017-12-30  0:51 ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1514476903-5206-1-git-send-email-b7.10110111@gmail.com \
    --to=b7.10110111@gmail.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).