diff options
author | Tom de Vries <tdevries@suse.de> | 2022-06-04 13:17:33 +0200 |
---|---|---|
committer | Tom de Vries <tdevries@suse.de> | 2022-06-04 13:17:33 +0200 |
commit | 1d8c0dfae79a5183e9e3311fb867afd679bc8e84 (patch) | |
tree | 2415ed745f9dc5a2828564fe0e90af01b93fcadb /gdb/c-exp.y | |
parent | 1b4633f812b329863a523c4d798c2b55417b5144 (diff) | |
download | gdb-1d8c0dfae79a5183e9e3311fb867afd679bc8e84.zip gdb-1d8c0dfae79a5183e9e3311fb867afd679bc8e84.tar.gz gdb-1d8c0dfae79a5183e9e3311fb867afd679bc8e84.tar.bz2 |
[gdb/c] Fix type of 2147483648 and literal truncation
[ Assuming arch i386:x86-64, sizeof (int) == 4,
sizeof (long) == sizeof (long long) == 8. ]
Currently we have (decimal for 0x80000000):
...
(gdb) ptype 2147483648
type = unsigned int
...
According to C language rules, unsigned types cannot be used for decimal
constants, so the type should be long instead (reported in PR16377).
Fix this by making sure the type of 2147483648 is long.
The next interesting case is (decimal for 0x8000000000000000):
...
(gdb) ptype 9223372036854775808
type = unsigned long
...
According to the same rules, unsigned long is incorrect.
Current gcc uses __int128 as type, which is allowed, but we don't have that
available in gdb, so the strict response here would be erroring out with
overflow.
Older gcc without __int128 support, as well as clang use an unsigned type, but with
a warning. Interestingly, clang uses "unsigned long long" while gcc uses
"unsigned long", which seems the better choice.
Given that the compilers allow this as a convience, do the same in gdb
and keep type "unsigned long", and make this explicit in parser and test-case.
Furthermore, make sure we error out on overflow instead of truncating in all
cases.
Tested on x86_64-linux with --enable-targets=all.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16377
Diffstat (limited to 'gdb/c-exp.y')
-rw-r--r-- | gdb/c-exp.y | 111 |
1 files changed, 44 insertions, 67 deletions
diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 72f8dd3..61a61fc 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -1929,7 +1929,6 @@ parse_number (struct parser_state *par_state, { ULONGEST n = 0; ULONGEST prevn = 0; - ULONGEST un; int i = 0; int c; @@ -1945,9 +1944,6 @@ parse_number (struct parser_state *par_state, /* We have found a "L" or "U" (or "i") suffix. */ int found_suffix = 0; - ULONGEST high_bit; - struct type *signed_type; - struct type *unsigned_type; char *p; p = (char *) alloca (len); @@ -2095,18 +2091,12 @@ parse_number (struct parser_state *par_state, if (i >= base) return ERROR; /* Invalid digit in this base */ - /* Portably test for overflow (only works for nonzero values, so make - a second check for zero). FIXME: Can't we just make n and prevn - unsigned and avoid this? */ - if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0) - unsigned_p = 1; /* Try something unsigned */ - - /* Portably test for unsigned overflow. - FIXME: This check is wrong; for example it doesn't find overflow - on 0x123456789 when LONGEST is 32 bits. */ - if (c != 'l' && c != 'u' && c != 'i' && n != 0) - { - if (unsigned_p && prevn >= n) + if (c != 'l' && c != 'u' && c != 'i') + { + /* Test for overflow. */ + if (prevn == 0 && n == 0) + ; + else if (prevn >= n) error (_("Numeric constant too large.")); } prevn = n; @@ -2123,58 +2113,45 @@ parse_number (struct parser_state *par_state, or gdbarch_long_bit will be that big, sometimes not. To deal with the case where it is we just always shift the value more than once, with fewer bits each time. */ - - un = n >> 2; - if (long_p == 0 - && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0) - { - high_bit - = ((ULONGEST)1) << (gdbarch_int_bit (par_state->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 (par_state)->builtin_unsigned_int; - signed_type = parse_type (par_state)->builtin_int; - } - else if (long_p <= 1 - && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0) - { - high_bit - = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1); - unsigned_type = parse_type (par_state)->builtin_unsigned_long; - signed_type = parse_type (par_state)->builtin_long; - } + 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 ()); + bool have_signed + /* No 'u' suffix. */ + = !unsigned_p; + bool have_unsigned + = ((/* 'u' suffix. */ + unsigned_p) + || (/* Not a decimal. */ + base != 10) + || (/* Allowed as a convenience, in case decimal doesn't fit in largest + signed type. */ + !fits_in_type (1, n, long_long_bits, true))); + bool have_int + /* No 'l' or 'll' suffix. */ + = long_p == 0; + bool have_long + /* No 'll' suffix. */ + = long_p <= 1; + if (have_int && have_signed && fits_in_type (1, n, int_bits, true)) + putithere->typed_val_int.type = parse_type (par_state)->builtin_int; + else if (have_int && have_unsigned && fits_in_type (1, n, int_bits, false)) + putithere->typed_val_int.type + = parse_type (par_state)->builtin_unsigned_int; + else if (have_long && have_signed && fits_in_type (1, n, long_bits, true)) + putithere->typed_val_int.type = parse_type (par_state)->builtin_long; + else if (have_long && have_unsigned && fits_in_type (1, n, long_bits, false)) + putithere->typed_val_int.type + = parse_type (par_state)->builtin_unsigned_long; + else if (have_signed && fits_in_type (1, n, long_long_bits, true)) + putithere->typed_val_int.type + = parse_type (par_state)->builtin_long_long; + else if (have_unsigned && fits_in_type (1, n, long_long_bits, false)) + putithere->typed_val_int.type + = parse_type (par_state)->builtin_unsigned_long_long; else - { - int shift; - if (sizeof (ULONGEST) * HOST_CHAR_BIT - < gdbarch_long_long_bit (par_state->gdbarch ())) - /* A long long does not fit in a LONGEST. */ - shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1); - else - shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1); - high_bit = (ULONGEST) 1 << shift; - unsigned_type = parse_type (par_state)->builtin_unsigned_long_long; - signed_type = parse_type (par_state)->builtin_long_long; - } - - putithere->typed_val_int.val = n; - - /* If the high bit of the worked out type is set then this number - has to be unsigned. */ - - if (unsigned_p || (n & high_bit)) - { - putithere->typed_val_int.type = unsigned_type; - } - else - { - putithere->typed_val_int.type = signed_type; - } + error (_("Numeric constant too large.")); + putithere->typed_val_int.val = n; if (imaginary_p) putithere->typed_val_int.type |