testmark-bash

Fix running code from a hunk name in error messages, on bash 4.4 through 5.1.

commit f110c869d078598f5bf67c8ee3ab620ec84da39b · parent 499e4121d7 · browse this tree
Ximianth, 2026-10-25 12:03:07 UTC

_testmark_hunk_err put an element keyed by the hunk name into arithmetic, $((start[$2] - 1)).
Bash 4.4 through 5.1 evaluate such a key again there, so a name like x$(cmd) ran cmd,
and other names failed with 'bad array subscript' or printed line -1.
The element is now copied out first, as docs/hacking/bash-pitfalls.md says.

tests/test-check.sh's syntax-names rows already catch this: they died on 4.4, 5.0, and 5.1 before this, and pass now.
It went unseen because the old-bash runs are by hand.
diff --git a/lib/testmark/document.bash b/lib/testmark/document.bash
index 8bdb920..c14461a 100644
--- a/lib/testmark/document.bash
+++ b/lib/testmark/document.bash
@@ -24,7 +24,9 @@ _testmark_err() {
 ## LINE is the hunk's annotation line, so a reader can go straight to it.
 _testmark_hunk_err() {
 	local -n file=_testmark_file_$1 start=_testmark_start_$1
-	_testmark_err "$file:$((start[$2] - 1)): hunk \"$2\": $3"
+	# Copied out first: an element keyed by a name must stay out of arithmetic; see bash-pitfalls.md.
+	local at=${start[$2]}
+	_testmark_err "$file:$((at - 1)): hunk \"$2\": $3"
 }
 
 ## Fails with a message unless $1 is a valid handle.