summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/parse_number.exp
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/testsuite/gdb.base/parse_number.exp')
-rw-r--r--gdb/testsuite/gdb.base/parse_number.exp105
1 files changed, 105 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.base/parse_number.exp b/gdb/testsuite/gdb.base/parse_number.exp
new file mode 100644
index 00000000000..ccef3f09655
--- /dev/null
+++ b/gdb/testsuite/gdb.base/parse_number.exp
@@ -0,0 +1,105 @@
+# Copyright 2022 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Test parsing numbers. Several language parsers had the same bug
+# around parsing large 64-bit numbers, hitting undefined behavior, and
+# thus crashing a GDB built with UBSan. This testcase goes over all
+# languages exercising printing the max 64-bit number, making sure
+# that GDB doesn't crash. ARCH is the architecture to test with.
+
+proc test_parse_numbers {arch} {
+ set arch_re [string_to_regexp $arch]
+ gdb_test "set architecture $arch" "The target architecture is set to \"$arch_re\"."
+
+ gdb_test_no_output "set language c"
+
+ # Types have different sizes depending on the architecture.
+ # Figure out type sizes before matching patterns in the upcoming
+ # tests.
+
+ set sizeof_long_long [get_sizeof "long long" -1]
+ set sizeof_long [get_sizeof "long" -1]
+ set sizeof_int [get_sizeof "int" -1]
+
+ if {$sizeof_long_long == 8 && $sizeof_long == 8} {
+ set 8B_type "unsigned long"
+ set fortran_type "unsigned long"
+ set fortran_value "0xffffffffffffffff"
+ } elseif {$sizeof_long_long == 8 && $sizeof_long == 4 && $sizeof_int == 4} {
+ set 8B_type "unsigned long long"
+ set fortran_type "unsigned int"
+ set fortran_value "0xffffffff"
+ } elseif {$sizeof_long == 4 && $sizeof_int == 2} {
+ set 8B_type "unsigned long long"
+ set fortran_type "unsigned long"
+ set fortran_value "0xffffffff"
+ } else {
+ error "missing case for long long = $sizeof_long_long, long = $sizeof_long, int = $sizeof_int"
+ }
+
+ foreach_with_prefix lang $::all_languages {
+ gdb_test_no_output "set language $lang"
+
+ set val "0xffffffffffffffff"
+ if {$lang == "fortran"} {
+ gdb_test "p/x $val" " = $fortran_value"
+ gdb_test "ptype $val" " = $fortran_type"
+ } elseif {$lang == "modula-2"} {
+ gdb_test "p/x 0FFFFFFFFFFFFFFFFH" "Overflow on numeric constant\\."
+ } elseif {$lang == "unknown"} {
+ gdb_test "p/x $val" \
+ "expression parsing not implemented for language \"Unknown\""
+ } else {
+ # D and Rust define their own built-in 64-bit types, and
+ # are thus always able to parse/print 64-bit values.
+ if {$sizeof_long_long == 4 && $lang != "d" && $lang != "rust"} {
+ set out "0xffffffff"
+ } else {
+ set out $val
+ }
+ gdb_test "p/x $val" " = $out"
+ if {$lang == "ada"} {
+ if {$sizeof_long_long == 4} {
+ gdb_test "ptype $val" " = <4-byte integer>"
+ } else {
+ gdb_test "ptype $val" " = <8-byte integer>"
+ }
+ } elseif {$lang == "d"} {
+ gdb_test "ptype $val" " = ulong"
+ } elseif {$lang == "rust"} {
+ gdb_test "ptype $val" " = i64"
+ } else {
+ gdb_test "ptype $val" " = $8B_type"
+ }
+ }
+ }
+}
+
+clean_restart
+
+gdb_test_no_output "set max-completions unlimited"
+
+set supported_archs [get_set_option_choices "set architecture"]
+# There should be at least one more than "auto".
+gdb_assert {[llength $supported_archs] > 1} "at least one architecture"
+
+set all_languages [get_set_option_choices "set language"]
+
+foreach_with_prefix arch $supported_archs {
+ if {$arch == "auto"} {
+ continue
+ }
+ test_parse_numbers $arch
+}