Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@redhat.com>
To: gdb-patches@sourceware.org
Subject: RFA: fix PR 7286
Date: Tue, 06 Jan 2009 00:52:00 -0000	[thread overview]
Message-ID: <m3k599gssd.fsf@fleche.redhat.com> (raw)

PR 7286 concerns the type of certain decimal integer constants.

According to C99, such a constant is never unsigned, but instead is of
the next wider type (from int, long, and long long) which can
represent its value.

I've included a rather roundabout test case.  I think this particular
boundary condition can only be tested where LONGEST is wider than the
target's word size.  As LONGEST is a define, and not a typedef, the
test determines the size of LONGEST by using a field whose type is
unlikely to change.

Another oddity here is the interpretation of the most negative 'long
long'.  I chose to leave this as unsigned, though it would also make
sense to make this an overflow error.  The latter seemed excessively
pedantic, though I don't feel strongly about this.

Please review.

thanks,
Tom

2009-01-05  Tom Tromey  <tromey@redhat.com>

	PR cli/7286:
	* c-exp.y (parse_number): Handle large decimal constants according
	to C99.

2009-01-05  Tom Tromey  <tromey@redhat.com>

	* gdb.gdb/selftest.exp (test_int_parsing): New proc.
	(test_with_self): Call it.

diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index d4bbbcc..dbb390e 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -1308,18 +1308,19 @@ parse_number (p, len, parsed_float, putithere)
      the case where it is we just always shift the value more than
      once, with fewer bits each time.  */
 
-  un = (ULONGEST)n >> 2;
+  /* A large decimal (not hex or octal) constant (between INT_MAX and
+     UINT_MAX) uses the smallest type that can hold its value.  So,
+     for decimal values, we shift one fewer bit, so that a value that
+     would overflow into the sign bit uses the next higher type.  */
+  if (base == 10)
+    un = (ULONGEST)n >> 1;
+  else
+    un = (ULONGEST)n >> 2;
   if (long_p == 0
       && (un >> (gdbarch_int_bit (parse_gdbarch) - 2)) == 0)
     {
       high_bit = ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch) - 1);
 
-      /* A large decimal (not hex or octal) constant (between INT_MAX
-	 and UINT_MAX) is a long or unsigned long, according to ANSI,
-	 never an unsigned int, but this code treats it as unsigned
-	 int.  This probably should be fixed.  GCC gives a warning on
-	 such constants.  */
-
       unsigned_type = parse_type->builtin_unsigned_int;
       signed_type = parse_type->builtin_int;
     }
diff --git a/gdb/testsuite/gdb.gdb/selftest.exp b/gdb/testsuite/gdb.gdb/selftest.exp
index 495ae45..4c9520e 100644
--- a/gdb/testsuite/gdb.gdb/selftest.exp
+++ b/gdb/testsuite/gdb.gdb/selftest.exp
@@ -247,6 +247,54 @@ proc do_steps_and_nexts {} {
     }
 }
 
+proc test_int_parsing {} {
+    global gdb_prompt expect_out
+
+    # Try to extract the size of LONGEST.  If LONGEST is wider than a
+    # pointer, then we can test a boundary case of C integer constant
+    # parsing without overflow.
+    gdb_test_multiple "print sizeof (((union exp_element *) 0)->longconst)" \
+      "determine size of LONGEST" {
+	  -re ".* = \(\[0-9\]+\).*$gdb_prompt $" {
+	      set longest_size $expect_out(1,string)
+	  }
+	  -re ".*$gdb_prompt $" {
+	      # Nothing.
+	  }
+      }
+    # Always pass.  If this fails, we don't want to hear about it.
+    pass "determine size of LONGEST"
+    if {! [info exists longest_size]} {
+	return
+    }
+
+    gdb_test_multiple "print sizeof (void *)" "determine size of void*" {
+	  -re ".* = \(\[0-9\]+\).*$gdb_prompt $" {
+	      set voidp_size $expect_out(1,string)
+	  }
+	  -re ".*$gdb_prompt $" {
+	      # Nothing.
+	  }
+    }
+    # Always pass.  If this fails, we don't want to hear about it.
+    pass "determine size of void*"
+    if {! [info exists voidp_size]} {
+	return
+    }
+
+    if {$longest_size <= $voidp_size} {
+	return
+    }
+
+    if {$voidp_size == 4} {
+	# 32-bit machine.
+	gdb_test "print -2147483648" " = -2147483648"
+    } elseif {$voidp_size == 8} {
+	# 64-bit machine.
+	gdb_test "print -9223372036854775808" " = -9223372036854775808"
+    }
+}
+
 proc test_with_self { executable } {
     global gdb_prompt
     global tool
@@ -279,6 +327,8 @@ proc test_with_self { executable } {
 	return -1
     }
 
+    test_int_parsing
+
     if { $gdb_file_cmd_debug_info != "debug" } then {
 	untested "No debug information, skipping testcase."
 	return -1


             reply	other threads:[~2009-01-06  0:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-06  0:52 Tom Tromey [this message]
2009-01-06  2:48 ` Joseph S. Myers
2009-01-06  3:25   ` Tom Tromey
2009-01-06  5:59     ` Joseph S. Myers
2009-01-07 19:34       ` Tom Tromey

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=m3k599gssd.fsf@fleche.redhat.com \
    --to=tromey@redhat.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