public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom de Vries <vries@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] [gdb/exp] Fix UB in scalar_binop
Date: Mon, 23 May 2022 12:50:12 +0000 (GMT)	[thread overview]
Message-ID: <20220523125012.B5CAF38F861C@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5a3cf18c2ed9593f194ea22f50ea5651532f6cfc

commit 5a3cf18c2ed9593f194ea22f50ea5651532f6cfc
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon May 23 14:50:02 2022 +0200

    [gdb/exp] Fix UB in scalar_binop
    
    When building gdb with -fsanitize=undefined, I run into:
    ...
    $ gdb -q -batch -ex "p -(-0x7fffffffffffffff - 1)"
    src/gdb/valarith.c:1385:10: runtime error: signed integer overflow: \
      0 - -9223372036854775808 cannot be represented in type 'long int'
    $1 = -9223372036854775808
    ...
    
    Fix this by performing the substraction in scalar_binop using unsigned types.
    
    Tested on x86_64-linux.

Diff:
---
 gdb/testsuite/gdb.base/arithmet.exp | 2 ++
 gdb/valarith.c                      | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/testsuite/gdb.base/arithmet.exp b/gdb/testsuite/gdb.base/arithmet.exp
index b6009a36235..4905c2e2706 100644
--- a/gdb/testsuite/gdb.base/arithmet.exp
+++ b/gdb/testsuite/gdb.base/arithmet.exp
@@ -98,3 +98,5 @@ gdb_test "print x-(y+w)"  "3"
 gdb_test "print x/(y*w)"  "0"
 gdb_test "print x-(y/w)"  "9"
 gdb_test "print (x+y)*w" "42"
+
+gdb_test "p /x -(-0x7fffffffffffffff - 1)" " = 0x8000000000000000"
diff --git a/gdb/valarith.c b/gdb/valarith.c
index 6210267826e..526cc02599e 100644
--- a/gdb/valarith.c
+++ b/gdb/valarith.c
@@ -1382,7 +1382,10 @@ scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
 	      break;
 
 	    case BINOP_SUB:
-	      v = v1 - v2;
+	      /* Avoid runtime error: signed integer overflow: \
+		 0 - -9223372036854775808 cannot be represented in type
+		 'long int'.  */
+	      v = (ULONGEST)v1 - (ULONGEST)v2;
 	      break;
 
 	    case BINOP_MUL:


                 reply	other threads:[~2022-05-23 12:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220523125012.B5CAF38F861C@sourceware.org \
    --to=vries@sourceware.org \
    --cc=gdb-cvs@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).