public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [committed][PATCH 2/2] [gdbsupport] Fix UB in print-utils.cc:int_string
Date: Mon, 23 May 2022 15:06:11 +0200	[thread overview]
Message-ID: <d450e88c-b936-cc52-b8a9-1a25d8a3f139@suse.de> (raw)
In-Reply-To: <20220517154048.13213-2-tdevries@suse.de>

On 5/17/22 17:40, Tom de Vries via Gdb-patches wrote:
> When building gdb with -fsanitize=undefined, I run into:
> ...
> (gdb) PASS: gdb.ada/access_to_packed_array.exp: set logging enabled on
> maint print symbols^M
> print-utils.cc:281:29:runtime error: negation of -9223372036854775808 cannot \
>    be represented in type 'long int'; cast to an unsigned type to negate this \
>    value to itself
> (gdb) FAIL: gdb.ada/access_to_packed_array.exp: maint print symbols
> ...
> 
> By running in a debug session, we find that this happens during printing of:
> ...
>     typedef system.storage_elements.storage_offset: \
>       range -9223372036854775808 .. 9223372036854775807;
> ...
> Possibly, an ada test-case could be created that exercises this in isolation.
> 
> The problem is here in int_string, where we negate a val with type LONGEST:
> ...
>           return decimal2str ("-", -val, width);
> ...
> 
> Fix this by, as recommend, using "-(ULONGEST)val" instead.
> 
> Tested on x86_64-linux.

Committed.

Thanks,
- Tom

> ---
>   gdbsupport/print-utils.cc | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/gdbsupport/print-utils.cc b/gdbsupport/print-utils.cc
> index 73ff1afda30..7bbb6deea74 100644
> --- a/gdbsupport/print-utils.cc
> +++ b/gdbsupport/print-utils.cc
> @@ -278,7 +278,11 @@ int_string (LONGEST val, int radix, int is_signed, int width,
>       case 10:
>         {
>   	if (is_signed && val < 0)
> -	  return decimal2str ("-", -val, width);
> +	  /* Cast to unsigned before negating, to prevent runtime error:
> +	     negation of -9223372036854775808 cannot be represented in type
> +	     'long int'; cast to an unsigned type to negate this value to
> +	     itself.  */
> +	  return decimal2str ("-", -(ULONGEST)val, width);
>   	else
>   	  return decimal2str ("", val, width);
>         }

  reply	other threads:[~2022-05-23 13:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-17 15:40 [PATCH 1/2] [gdb/exp] Fix UB in scalar_binop Tom de Vries
2022-05-17 15:40 ` [PATCH 2/2] [gdbsupport] Fix UB in print-utils.cc:int_string Tom de Vries
2022-05-23 13:06   ` Tom de Vries [this message]
2022-05-23 13:04 ` [committed][PATCH 1/2] [gdb/exp] Fix UB in scalar_binop Tom de Vries

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=d450e88c-b936-cc52-b8a9-1a25d8a3f139@suse.de \
    --to=tdevries@suse.de \
    --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).