[PATCH 1/3][gdb/testsuite] Fix global array cp_class_table_history leak

Tom de Vries
2020-05-19T16:29:23Z

Hi,

Test-cases using proc cp_test_ptype_class set a global array
cp_class_table_history, which remains set after the test-case has run. This
has the potential to:
- cause tcl errors, as well as
- reuse results from one test-case in another test-case when that is not
appropriate

Fix this by:
- moving the variable into a namespace, and
- only using the variable in the test-cases that need it, and
- resetting the variable at the end of those test-cases.

Any comments?

Thanks,
- Tom

[gdb/testsuite] Fix global array cp_class_table_history leak

gdb/testsuite/ChangeLog:

2020-05-19 Tom de Vries <tdevries@suse.de>

PR testsuite/25996
* lib/cp-support.exp (init_cp_test_ptype_class_cache)
(finish_cp_test_ptype_class_cache): New proc.
(cp_test_ptype_class): Only use use_cp_class_table_history if
init_cp_test_ptype_class_cache was called.
* gdb.cp/inherit.exp: Call init_cp_test_ptype_class_cache and
finish_cp_test_ptype_class_cache.
* gdb.cp/virtfunc.exp: Same.

---
gdb/testsuite/gdb.cp/inherit.exp | 6 +++++
gdb/testsuite/gdb.cp/virtfunc.exp | 6 +++++
gdb/testsuite/lib/cp-support.exp | 53 +++++++++++++++++++++++++++++++--------
3 files changed, 55 insertions(+), 10 deletions(-)

diff --git a/gdb/testsuite/gdb.cp/inherit.exp b/gdb/testsuite/gdb.cp/inherit.exp
index 9616015709..29d4880999 100644
--- a/gdb/testsuite/gdb.cp/inherit.exp
+++ b/gdb/testsuite/gdb.cp/inherit.exp
@@ -711,4 +711,10 @@ proc do_tests { } {
test_print_mvi_classes
}

+# This test-case uses the cp_test_ptype_class cache (by means of the ibid
+# argument), so initialize and finalize the cache.
+init_cp_test_ptype_class_cache
+
do_tests
+
+finish_cp_test_ptype_class_cache
diff --git a/gdb/testsuite/gdb.cp/virtfunc.exp b/gdb/testsuite/gdb.cp/virtfunc.exp
index 2b046c5759..214ab1ec22 100644
--- a/gdb/testsuite/gdb.cp/virtfunc.exp
+++ b/gdb/testsuite/gdb.cp/virtfunc.exp
@@ -289,4 +289,10 @@ proc do_tests {} {
gdb_test "step" ".*E::vg.*" "step through thunk into E::vg"
}

+# This test-case uses the cp_test_ptype_class cache (by means of the ibid
+# argument), so initialize and finalize the cache.
+init_cp_test_ptype_class_cache
+
do_tests
+
+finish_cp_test_ptype_class_cache
diff --git a/gdb/testsuite/lib/cp-support.exp b/gdb/testsuite/lib/cp-support.exp
index b4a5582742..4142b00d8e 100644
--- a/gdb/testsuite/lib/cp-support.exp
+++ b/gdb/testsuite/lib/cp-support.exp
@@ -255,17 +255,22 @@ proc cp_test_ptype_class { in_exp in_testname in_key in_tag in_class_table
set in_command "ptype${in_ptype_arg} $in_exp"
}

- # Save class tables in a history array for reuse.
-
- global cp_class_table_history
- if { $in_class_table == "ibid" } then {
- if { ! [info exists cp_class_table_history("$in_key,$in_tag") ] } then {
- fail "$in_testname // bad ibid"
- return false
+ namespace upvar ::cp_support_internal:: use_cp_class_table_history \
+ use_cp_class_table_history
+ if { [info exists use_cp_class_table_history ] } {
+ # Save class tables in a history array for reuse.
+
+ namespace upvar ::cp_support_internal:: cp_class_table_history \
+ cp_class_table_history
+ if { $in_class_table == "ibid" } then {
+ if { ! [info exists cp_class_table_history("$in_key,$in_tag") ] } then {
+ fail "$in_testname // bad ibid"
+ return false
+ }
+ set in_class_table $cp_class_table_history("$in_key,$in_tag")
+ } else {
+ set cp_class_table_history("$in_key,$in_tag") $in_class_table
}
- set in_class_table $cp_class_table_history("$in_key,$in_tag")
- } else {
- set cp_class_table_history("$in_key,$in_tag") $in_class_table
}

# Split the class table into separate tables.
@@ -764,3 +769,31 @@ proc cp_test_ptype_class { in_exp in_testname in_key in_tag in_class_table

return true
}
+
+# Initialize the cp_test_ptype_class cache.
+
+proc init_cp_test_ptype_class_cache { } {
+ namespace upvar ::cp_support_internal:: use_cp_class_table_history \
+ use_cp_class_table_history
+ set use_cp_class_table_history 1
+
+ namespace upvar ::cp_support_internal:: cp_class_table_history \
+ cp_class_table_history
+ if { [info exists cp_class_table_history] } {
+ unset cp_class_table_history
+ }
+}
+
+# Finalize the cp_test_ptype_class cache.
+
+proc finish_cp_test_ptype_class_cache { } {
+ namespace upvar ::cp_support_internal:: use_cp_class_table_history \
+ use_cp_class_table_history
+ unset use_cp_class_table_history
+
+ namespace upvar ::cp_support_internal:: cp_class_table_history \
+ cp_class_table_history
+ if { [info exists cp_class_table_history] } {
+ unset cp_class_table_history
+ }
+}