diff options
author | Pedro Alves <pedro@palves.net> | 2022-04-07 18:01:12 +0100 |
---|---|---|
committer | Pedro Alves <pedro@palves.net> | 2022-04-08 16:19:15 +0100 |
commit | 01772c548b91499272dcb0f6864ba990e2abf873 (patch) | |
tree | ea4bf0aad239a0dbb8e2a2876ae9e59cae501a00 /gdb/p-exp.y | |
parent | 7d41169b6d4dbc3950516d0cdf0d8ca462b52bbb (diff) | |
download | fsf-binutils-gdb-01772c548b91499272dcb0f6864ba990e2abf873.zip fsf-binutils-gdb-01772c548b91499272dcb0f6864ba990e2abf873.tar.gz fsf-binutils-gdb-01772c548b91499272dcb0f6864ba990e2abf873.tar.bz2 |
Fix undefined behavior in the Fortran, Go and Pascal number parsers
This commit ports these two fixes to the C parser:
commit ebf13736b42af47c9907b5157c8e80c78dbe00e1
CommitDate: Thu Sep 4 21:46:28 2014 +0100
parse_number("0") reads uninitialized memory
commit 20562150d8a894bc91657c843ee88c508188e32e
CommitDate: Wed Oct 3 15:19:06 2018 -0600
Avoid undefined behavior in parse_number
... to the Fortran, Go, and Fortran number parsers, fixing the same
problems there.
Also add a new testcase that exercises printing 0xffffffffffffffff
(max 64-bit) in all languages, which crashes a GDB built with UBsan
without the fix.
I moved get_set_option_choices out of all-architectures.exp.tcl to
common code to be able to extract all the supported languages. I did
a tweak to it to generalize it a bit -- you now have to pass down the
"set" part of the command as well. This is so that the proc can be
used with "maintenance set" commands as well in future.
Change-Id: I8e8f2fdc1e8407f63d923c26fd55d98148b9e16a
Diffstat (limited to 'gdb/p-exp.y')
-rw-r--r-- | gdb/p-exp.y | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/gdb/p-exp.y b/gdb/p-exp.y index adceff2..3d1ea72 100644 --- a/gdb/p-exp.y +++ b/gdb/p-exp.y @@ -803,10 +803,8 @@ static int parse_number (struct parser_state *par_state, const char *p, int len, int parsed_float, YYSTYPE *putithere) { - /* FIXME: Shouldn't these be unsigned? We don't deal with negative values - here, and we do kind of silly things like cast to unsigned. */ - LONGEST n = 0; - LONGEST prevn = 0; + ULONGEST n = 0; + ULONGEST prevn = 0; ULONGEST un; int i = 0; @@ -855,7 +853,7 @@ parse_number (struct parser_state *par_state, } /* Handle base-switching prefixes 0x, 0t, 0d, 0. */ - if (p[0] == '0') + if (p[0] == '0' && len > 1) switch (p[1]) { case 'x': @@ -933,7 +931,7 @@ parse_number (struct parser_state *par_state, on 0x123456789 when LONGEST is 32 bits. */ if (c != 'l' && c != 'u' && n != 0) { - if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n)) + if (unsigned_p && prevn >= n) error (_("Numeric constant too large.")); } prevn = n; @@ -951,7 +949,7 @@ parse_number (struct parser_state *par_state, the case where it is we just always shift the value more than once, with fewer bits each time. */ - un = (ULONGEST)n >> 2; + un = n >> 2; if (long_p == 0 && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0) { |