]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit - gdb/c-exp.y
[gdb/c] Fix type of 2147483648 and literal truncation
authorTom de Vries <tdevries@suse.de>
Sat, 4 Jun 2022 11:17:33 +0000 (13:17 +0200)
committerTom de Vries <tdevries@suse.de>
Sat, 4 Jun 2022 11:17:33 +0000 (13:17 +0200)
commit1d8c0dfae79a5183e9e3311fb867afd679bc8e84
tree2415ed745f9dc5a2828564fe0e90af01b93fcadb
parent1b4633f812b329863a523c4d798c2b55417b5144
[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
gdb/c-exp.y
gdb/parse.c
gdb/parser-defs.h
gdb/testsuite/gdb.base/parse_number.exp