diff options
Diffstat (limited to 'gdb')
-rw-r--r-- | gdb/testsuite/gdb.ada/uninitialized-variable-record.exp | 122 | ||||
-rw-r--r-- | gdb/testsuite/gdb.ada/uninitialized-variable-record/parse.adb | 33 | ||||
-rw-r--r-- | gdb/value.c | 3 |
3 files changed, 158 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.ada/uninitialized-variable-record.exp b/gdb/testsuite/gdb.ada/uninitialized-variable-record.exp new file mode 100644 index 0000000..7fc7239 --- /dev/null +++ b/gdb/testsuite/gdb.ada/uninitialized-variable-record.exp @@ -0,0 +1,122 @@ +# Copyright 2024 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/>. + +load_lib "ada.exp" + +require allow_ada_tests + +standard_ada_testfile parse + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable {debug}] != "" } { + return -1 +} + +clean_restart ${testfile} + +set bp_location [gdb_get_line_number "START" ${testdir}/parse.adb] +runto "parse.adb:$bp_location" + +# Check that we have the expected value for variable y2. + +gdb_test "p y2" [string_to_regexp " = (a => false, c => 1.0, d => 2)"] + +# Shorthand. + +proc set_lang { lang } { + gdb_test_multiple "set language $lang" "" { + -re -wrap "" { + } + } +} + +# Calculate the offset of y2.d. + +set re_cast [string_to_regexp "(access integer)"] +gdb_test_multiple "print &y2.d - &y2" "" { + -re -wrap " = $re_cast ($hex)" { + set offset_d $expect_out(1,string) + pass $gdb_test_name + } +} + +# Try to find a interesting discriminator value, such that at the same time: +# - the d field is part of the variable, and +# - the type size is too small to contain d. + +set interesting_discriminator -1 +set_lang c +for { set i 0 } { $i < 256 } { incr i } { + with_test_prefix $i { + + # Patch in the discriminator value. + gdb_test_multiple "set var *(unsigned char *)(&y2.a)=$i" "" { + -re -wrap "" { + } + } + + # Check that we have the variant with fields c+d instead of b. + set have_b 0 + gdb_test_multiple "with language ada -- print y2.b" "" { + -re -wrap " = $decimal" { + set have_b 1 + } + -re -wrap "" { + } + } + if { $have_b } { + # This is the variant with field b. + continue + } + + set size 0 + gdb_test_multiple "print sizeof (y2)" "" { + -re -wrap " = (.*)" { + set size $expect_out(1,string) + } + } + + if { ! $size } { + continue + } + + if { [expr $size > $offset_d] } { + # Field d fits in the size. + continue + } + + set interesting_discriminator $i + break + } +} + +require {expr $interesting_discriminator != -1} + +foreach lang [gdb_supported_languages] { + with_test_prefix $lang { + set_lang $lang + + gdb_test_multiple "print y2" "" { + -re -wrap ", d => $decimal.*" { + fail $gdb_test_name + } + -re -wrap ", d = $decimal.*" { + fail $gdb_test_name + } + -re -wrap "" { + pass $gdb_test_name + } + } + } +} diff --git a/gdb/testsuite/gdb.ada/uninitialized-variable-record/parse.adb b/gdb/testsuite/gdb.ada/uninitialized-variable-record/parse.adb new file mode 100644 index 0000000..f00c75c --- /dev/null +++ b/gdb/testsuite/gdb.ada/uninitialized-variable-record/parse.adb @@ -0,0 +1,33 @@ +-- Copyright 2009-2024 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/>. + +-- Based on gdb.ada/uninitialized_vars/parse.adb. + +procedure Parse is + + type Variable_Record (A : Boolean := True) is record + case A is + when True => + B : Integer; + when False => + C : Float; + D : Integer; + end case; + end record; + Y2 : Variable_Record := (A => False, C => 1.0, D => 2); + +begin + null; -- START +end Parse; diff --git a/gdb/value.c b/gdb/value.c index 8840aa4..bea34e5 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -1229,6 +1229,9 @@ value::contents_copy_raw (struct value *dst, LONGEST dst_offset, gdb_assert (!dst->bits_any_optimized_out (TARGET_CHAR_BIT * dst_offset, TARGET_CHAR_BIT * length)); + if ((src_offset + copy_length) * unit_size > enclosing_type ()-> length ()) + error (_("access outside bounds of object")); + /* Copy the data. */ gdb::array_view<gdb_byte> dst_contents = dst->contents_all_raw ().slice (dst_offset * unit_size, |