Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [pushed 4/7] [gdb/testsuite] Add Term::check_size
Date: Wed, 14 Oct 2026 16:58:35 +0200	[thread overview]
Message-ID: <20261014145838.3560108-5-tdevries@suse.de> (raw)
In-Reply-To: <20261014145838.3560108-1-tdevries@suse.de>

Add a proc Term::check_size that checks that tuiterm doesn't grow beyond its
dimensions, and that the cursor stays within bounds.

Use it in test-case gdb.tui/tuiterm.exp, and fix the fallout in
lib/tuiterm.exp.

Add a few more corner-case tests in gdb.tui/tuiterm.exp, and fix those as
well.

Also replace an "unset _chars" with "array unset _chars" to make
Term::_do_resize more robust.
---
 gdb/testsuite/gdb.tui/tuiterm.exp | 42 ++++++++++++++++++
 gdb/testsuite/lib/tuiterm.exp     | 72 ++++++++++++++++++++++++++++---
 2 files changed, 109 insertions(+), 5 deletions(-)

diff --git a/gdb/testsuite/gdb.tui/tuiterm.exp b/gdb/testsuite/gdb.tui/tuiterm.exp
index f09e1514e9b..411026457b9 100644
--- a/gdb/testsuite/gdb.tui/tuiterm.exp
+++ b/gdb/testsuite/gdb.tui/tuiterm.exp
@@ -337,6 +337,15 @@ proc test_cursor_position { } {
 
     Term::_csi_H 3 5
     check "cursor horizontal absolute 2" $::small_initial 4 2
+
+    Term::_csi_H 10 10
+    check "cursor position absolute, out of bounds" $::small_initial 7 3
+
+    Term::_csi_H
+    check "cursor position absolute, default" $::small_initial 0 0
+
+    Term::_csi_H 0 0
+    check "cursor position absolute, zero" $::small_initial 0 0
 }
 
 proc test_cursor_horizontal_forward_tabulation { } {
@@ -494,6 +503,15 @@ proc test_erase_character { } {
 	"qrs uvwx"
 	"y    34 "
     } 1 3
+
+    Term::_move_cursor 1 0
+    Term::_csi_X 20
+    check "erase character, more than number of columns" {
+	"a       "
+	"ijklmnop"
+	"qrs uvwx"
+	"y    34 "
+    } 1 0
 }
 
 proc test_cursor_backward_tabulation { } {
@@ -571,6 +589,18 @@ proc test_insert_line { } {
     } 2 0
 }
 
+proc test_insert_line_2 { } {
+    # As in test_insert_line, but start after row 0.
+    Term::_move_cursor 0 1
+    Term::_csi_L 12
+    check "insert line after first line, insert more lines than display has" {
+	"abcdefgh"
+	"        "
+	"        "
+	"        "
+    } 0 1
+}
+
 proc test_attrs {} {
     foreach { attr vals } {
 	reverse {
@@ -682,11 +712,20 @@ proc_with_prefix test_with_tuiterm {} {
     }
 }
 
+proc_with_prefix test_resize {} {
+    setup_terminal 8 4
+    Term::_move_cursor 7 3
+    Term::_do_resize 2 4
+    check_cursor_position "cursor after resize" 3 1
+    Term::check_size
+}
+
 # Run proc TEST_PROC_NAME with a "small" terminal.
 
 proc run_one_test_small { test_proc_name } {
     setup_small
     $test_proc_name
+    Term::check_size
 }
 
 # Run proc TEST_PROC_NAME with a "large" terminal.
@@ -694,6 +733,7 @@ proc run_one_test_small { test_proc_name } {
 proc run_one_test_large { test_proc_name } {
     setup_large
     $test_proc_name
+    Term::check_size
 }
 
 foreach_with_prefix test {
@@ -718,6 +758,7 @@ foreach_with_prefix test {
     test_repeat
     test_vertical_line_position_absolute
     test_insert_line
+    test_insert_line_2
     test_pan_up
     test_pan_down
 } {
@@ -735,3 +776,4 @@ test_attrs
 
 test_with_term
 test_with_tuiterm
+test_resize
diff --git a/gdb/testsuite/lib/tuiterm.exp b/gdb/testsuite/lib/tuiterm.exp
index 62025857651..71682785eea 100644
--- a/gdb/testsuite/lib/tuiterm.exp
+++ b/gdb/testsuite/lib/tuiterm.exp
@@ -222,6 +222,8 @@ proc Term::_csi_@ {args} {
 	variable _cols
 	variable _chars
 
+	set n [expr {min($n, $_cols - $_cur_col)}]
+
 	# Move characters right of the cursor right by N positions,
 	# starting with the rightmost one.
 	for {set in_col [expr $_cols - $n - 1]} {$in_col >= $_cur_col} {incr in_col -1} {
@@ -351,6 +353,13 @@ proc Term::_csi_H {args} {
     _log_cur "Cursor Position ($row, $col)" {
 	variable _cur_col
 	variable _cur_row
+	variable _rows
+	variable _cols
+
+	set row [expr {min($row, $_rows)}]
+	set col [expr {min($col, $_cols)}]
+	set row [expr {max($row, 1)}]
+	set col [expr {max($col, 1)}]
 
 	set _cur_row [expr {$row - 1}]
 	set _cur_col [expr {$col - 1}]
@@ -425,6 +434,45 @@ proc Term::_csi_K {args} {
     }
 }
 
+# Check that the tuiterm doesn't grow beyond its dimensions, and that the
+# cursor stays in bounds.
+proc Term::check_size {} {
+    variable _chars
+    variable _cols
+    variable _rows
+    variable _cur_col
+    variable _cur_row
+
+    if { ![array exists _chars] } {
+	# Not set up yet.
+	return
+    }
+
+    if { ! ($_cur_row >= 0 && $_cur_col >= 0
+	    && $_cur_row < $_rows && $_cur_col < $_cols) } {
+	set pos "($_cur_row, $_cur_col)"
+	set dims "($_rows, $_cols)"
+	error "cursor out of bounds: $pos in $dims"
+    }
+
+    if {[array size _chars] != $_rows * $_cols} {
+	verbose -log "size: [array size _chars]"
+	verbose -log "rows: $_rows"
+	verbose -log "cols: $_cols"
+	verbose -log "rows * cols: [expr {$_cols * $_rows}]"
+	foreach elem [array names _chars] {
+	    set res [split $elem ,]
+	    set col [lindex $res 0]
+	    set row [lindex $res 1]
+	    if { !($row >= 0 && $col >= 0
+		   && $row < $_rows && $col < $_cols) } {
+		verbose -log "$elem"
+	    }
+	}
+	error "_chars has incorrect size"
+    }
+}
+
 # Insert Line
 #
 # https://vt100.net/docs/vt510-rm/IL.html
@@ -435,8 +483,10 @@ proc Term::_csi_L {args} {
 	variable _cur_row
 	variable _rows
 
-	set y [expr $_rows - 2]
-	set next_y [expr $y + $arg]
+	set arg [expr {min($arg, $_rows - $_cur_row)}]
+
+	set y [expr {$_rows - $arg - 1}]
+	set next_y [expr {$_rows - 1}]
 	while {$y >= $_cur_row} {
 	    _copy_line $y $next_y
 	    incr y -1
@@ -532,14 +582,16 @@ proc Term::_csi_T {args} {
     _log_cur "Pan Up ($count)" {
 	variable _rows
 
+	set count [expr {min($count, $_rows)}]
+
 	# The following code is written without consideration for
 	# the scroll margins.  At this time this comment was
 	# written the tuiterm library doesn't support the scroll
 	# margins.  If/when that changes, then the following will
 	# need to be updated.
 
-	set y [expr $_rows - $count]
-	set dy $_rows
+	set y [expr {$_rows - $count - 1}]
+	set dy [expr {$_rows - 1}]
 
 	while {$dy >= $count} {
 	    _copy_line $y $dy
@@ -563,6 +615,9 @@ proc Term::_csi_X {args} {
 	variable _cur_row
 	variable _attrs
 	variable _chars
+	variable _cols
+
+	set n [expr {min($n, $_cols - $_cur_col)}]
 
 	set lattr [array get _attrs]
 	set x $_cur_col
@@ -977,6 +1032,7 @@ proc Term::_setup {rows cols} {
     variable _cur_row
     variable _attrs
     variable _resize_count
+    variable _chars
 
     set _rows $rows
     set _cols $cols
@@ -985,6 +1041,7 @@ proc Term::_setup {rows cols} {
     set _resize_count 0
     _reset_attrs _attrs
 
+    array unset _chars
     _clear_lines 0 $_rows
 }
 
@@ -1668,13 +1725,15 @@ proc Term::_do_resize {rows cols} {
     variable _chars
     variable _rows
     variable _cols
+    variable _cur_row
+    variable _cur_col
 
     set old_rows [expr {min ($_rows, $rows)}]
     set old_cols [expr {min ($_cols, $cols)}]
 
     # Copy locally.
     array set local_chars [array get _chars]
-    unset _chars
+    array unset _chars
 
     set _rows $rows
     set _cols $cols
@@ -1685,6 +1744,9 @@ proc Term::_do_resize {rows cols} {
 	    set _chars($x,$y) $local_chars($x,$y)
 	}
     }
+
+    set _cur_row [expr {min($_cur_row, $_rows - 1)}]
+    set _cur_col [expr {min($_cur_col, $_cols - 1)}]
 }
 
 proc Term::resize {rows cols {wait_for_msg 1}} {
-- 
2.51.0


  parent reply	other threads:[~2026-10-14 15:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14 14:58 [pushed 0/7] [gdb/testsuite] Tuiterm maintenance Tom de Vries
2026-10-14 14:58 ` [pushed 1/7] [gdb/testsuite] Remove unused declarations in lib/tuiterm.exp Tom de Vries
2026-10-14 14:58 ` [pushed 2/7] [gdb/testsuite] Factor out Term::_copy_line Tom de Vries
2026-10-14 14:58 ` [pushed 3/7] [gdb/testsuite] Factor out small_initial in gdb.tui/tuiterm.exp Tom de Vries
2026-10-14 14:58 ` Tom de Vries [this message]
2026-10-14 14:58 ` [pushed 5/7] [gdb/testsuite] Work around namespace problem in tclint plugin Tom de Vries
2026-10-14 14:58 ` [pushed 6/7] [gdb/testsuite] Fix typo in tclint-plugin.py Tom de Vries
2026-10-14 14:58 ` [pushed 7/7] [gdb/testsuite] Fix test names in gdb.tui/tuiterm.exp Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261014145838.3560108-5-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox