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/ada] Fix literal truncation
Date: Sat,  4 Jun 2022 11:18:14 +0000 (GMT)	[thread overview]
Message-ID: <20220604111814.28B6C3839425@sourceware.org> (raw)

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

commit ac3afe36d73c84096685fece885d70b28bc9629f
Author: Tom de Vries <tdevries@suse.de>
Date:   Sat Jun 4 13:17:33 2022 +0200

    [gdb/ada] Fix literal truncation
    
    Make sure we error out on overflow instead of truncating in all cases.
    
    Tested on x86_64-linux, with a build with --enable-targets=all.

Diff:
---
 gdb/ada-lex.l                           | 27 +++++++++++++++++++++++----
 gdb/testsuite/gdb.base/parse_number.exp |  4 +---
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/gdb/ada-lex.l b/gdb/ada-lex.l
index 33a08eaa93b..002eb811e41 100644
--- a/gdb/ada-lex.l
+++ b/gdb/ada-lex.l
@@ -466,12 +466,16 @@ processInt (struct parser_state *par_state, const char *base0,
   if (mpz_cmp (result.val, maxval.val) > 0)
     error (_("Integer literal out of range"));
 
+  int int_bits = gdbarch_int_bit (par_state->gdbarch ());
+  int long_bits = gdbarch_long_bit (par_state->gdbarch ());
+  int long_long_bits = gdbarch_long_long_bit (par_state->gdbarch ());
+
   ULONGEST value = result.as_integer<ULONGEST> ();
-  if ((value >> (gdbarch_int_bit (par_state->gdbarch ())-1)) == 0)
+  if (fits_in_type (1, value, int_bits, true))
     yylval.typed_val.type = type_int (par_state);
-  else if ((value >> (gdbarch_long_bit (par_state->gdbarch ())-1)) == 0)
+  else if (fits_in_type (1, value, long_bits, true))
     yylval.typed_val.type = type_long (par_state);
-  else if (((value >> (gdbarch_long_bit (par_state->gdbarch ())-1)) >> 1) == 0)
+  else if (fits_in_type (1, value, long_bits, false))
     {
       /* We have a number representable as an unsigned integer quantity.
          For consistency with the C treatment, we will treat it as an
@@ -490,8 +494,23 @@ processInt (struct parser_state *par_state, const char *base0,
 	yylval.typed_val.val = (LONGEST) value;
       return INT;
     }
-  else
+  else if (fits_in_type (1, value, long_long_bits, true))
     yylval.typed_val.type = type_long_long (par_state);
+  else if (fits_in_type (1, value, long_long_bits, false))
+    {
+      /* Note: Interprets ULLONG_MAX as -1.  */
+      yylval.typed_val.type = type_long_long (par_state);
+      /* See unsigned long case above.  */
+      if (value & LONGEST_SIGN)
+	yylval.typed_val.val =
+	  (LONGEST) (value & ~LONGEST_SIGN)
+	  - (LONGEST_SIGN>>1) - (LONGEST_SIGN>>1);
+      else
+	yylval.typed_val.val = (LONGEST) value;
+      return INT;
+    }
+  else
+    error (_("Integer literal out of range"));
 
   yylval.typed_val.val = value;
   return INT;
diff --git a/gdb/testsuite/gdb.base/parse_number.exp b/gdb/testsuite/gdb.base/parse_number.exp
index 6e0091278a9..70b0ad065d7 100644
--- a/gdb/testsuite/gdb.base/parse_number.exp
+++ b/gdb/testsuite/gdb.base/parse_number.exp
@@ -146,9 +146,7 @@ proc parse_number { lang n } {
 	    return [list "<$sizeof_long_long-byte integer>" $n]
 	} else {
 	    # Overflow.
-	    # Some truncated value or re_overflow, should be re_overflow.
-	    return [list "($re_overflow|<$decimal-byte integer>)" \
-			($re_overflow|$any)]
+	    return [list $re_overflow $re_overflow]
 	}
     } elseif { $lang == "modula-2" } {
 	if { [string equal $n -0] } {


                 reply	other threads:[~2022-06-04 11:18 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=20220604111814.28B6C3839425@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).