public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <andrew.burgess@embecosm.com>
To: Shahab Vahedi <shahab.vahedi@gmail.com>
Cc: gdb-patches@sourceware.org, gdb@sourceware.org,
	Shahab Vahedi <shahab@synopsys.com>,
	Claudiu Zissulescu <claziss@synopsys.com>,
	Francois Bedard <fbedard@synopsys.com>
Subject: Re: [PATCH] GDB: Fix the overflow in addr_is_displayed()
Date: Mon, 06 Jan 2020 00:30:00 -0000	[thread overview]
Message-ID: <20200106003041.GS3865@embecosm.com> (raw)
In-Reply-To: <20200104114312.165656-1-shahab.vahedi@gmail.com>

* Shahab Vahedi <shahab.vahedi@gmail.com> [2020-01-04 12:43:12 +0100]:

> From: Shahab Vahedi <shahab@synopsys.com>
> 
> In a corner case scenario, where the height of the assembly TUI is
> bigger than the number of instructions in the whole program, GDB
> dumps core. The problem roots in this condition check:
> 
>   int i = 0;
>   while (i < content. size() - threshold ...) {
>     ... content[i] ...
>   }
> 
> "threshold" is 2 and there are times that "content. size()" is 0.
> This results into an overflow and the loop is entered whereas it
> should have been skipped.

I didn't quite understand the problem description.

I can see how 'content.size() - threshold' would overflow when the
size is 0 or 1, but I don't understand the part about the tui height
being bigger than the number of instructions.

I tried to reproduce the failure on native x86-64 but failed.  I can
get the initial stage reproduced, where the asm window is empty and I
see the error about "Cannot access memory at address ....", but then
when I attach to a remote the asm window fills in fine.  I guess this
is because on Linux the page the code is on is readable, and under
QEMU only the specific program instructions are readable maybe?

Anyway, the change looks reasonable, though I had one comment, inline
below...

> 
> This has been discussed at length in bug 25345:
>   https://sourceware.org/bugzilla/show_bug.cgi?id=25345
> 
> gdb/ChangeLog:
> 2020-01-04  Shahab Vahedi  <shahab@synopsys.com>
>         * tui/tui-disasm.c (tui_disasm_window::addr_is_displayed):
>         Treat "content.size()" as "int" to avoid overflow.
> ---
>  gdb/tui/tui-disasm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c
> index c72b50730b0..a0921eb09d1 100644
> --- a/gdb/tui/tui-disasm.c
> +++ b/gdb/tui/tui-disasm.c
> @@ -349,10 +349,10 @@ bool
>  tui_disasm_window::addr_is_displayed (CORE_ADDR addr) const
>  {
>    bool is_displayed = false;
> -  int threshold = SCROLL_THRESHOLD;
> +  int nr_of_lines = int(content.size()) - int(SCROLL_THRESHOLD);

I had a look through our code and I couldn't find any examples of us
using 'int (xxx)' syntax to cast to int, this is probably a result of
our C heritage, but we should probably try to be consistent I think.
I'd suggest maybe this become:

  int nr_of_lines = (int) content.size () - SCROLL_THRESHOLD;

Thanks,
Andrew

>  
>    int i = 0;
> -  while (i < content.size () - threshold && !is_displayed)
> +  while (i < nr_of_lines && !is_displayed)
>      {
>        is_displayed
>  	= (content[i].line_or_addr.loa == LOA_ADDRESS
> -- 
> 2.24.1
> 

      reply	other threads:[~2020-01-06  0:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-04 11:43 Shahab Vahedi
2020-01-06  0:30 ` Andrew Burgess [this message]

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=20200106003041.GS3865@embecosm.com \
    --to=andrew.burgess@embecosm.com \
    --cc=claziss@synopsys.com \
    --cc=fbedard@synopsys.com \
    --cc=gdb-patches@sourceware.org \
    --cc=gdb@sourceware.org \
    --cc=shahab.vahedi@gmail.com \
    --cc=shahab@synopsys.com \
    /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).