Skip to content

Commit e3aec53

Browse files
committed
tests: keep the diagram guard's scratch files in a private directory
check-diagram-data passed findings from two subshells back to itself through /tmp/.kd.$$ and /tmp/.vs.$$ — predictable names in the shared temporary directory, created by plain redirection and then read back. Another user could pre-create either path as a symlink and have the guard write through it, and control what it read afterwards; the following `rm -f` unlinks the symlink and leaves the write in place. Allocate one mktemp -d and put both files inside it: the directory is private, so names derived within it need no guarantee of their own. A trap removes the directory on EXIT, INT, TERM and HUP, so the files no longer survive an early exit either, and the explicit rm calls are gone. The comment now records why the files exist at all — a `while` loop on the right of a pipe runs in a subshell, and cannot set `fail` in the shell that reports it.
1 parent 6e30a6b commit e3aec53

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

tests/check-diagram-data

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ else
4545
fi
4646

4747
cd "$ROOT" || exit 2
48+
49+
# Scratch for the two hand-offs below: a `while` loop on the right of a pipe
50+
# runs in a subshell, so a finding recorded there cannot set `fail` in this
51+
# shell and is passed back through a file instead. The files live in a private
52+
# directory rather than under a predictable name in the shared temporary
53+
# directory, where another user could pre-create the path as a symlink for the
54+
# redirection to write through. mktemp -d gives a 0700 directory nobody else can
55+
# create in, which makes the names inside it safe to derive.
56+
TMP=$(mktemp -d "${TMPDIR:-/tmp}/kasld-diagram.XXXXXX") || {
57+
printf '%scheck-diagram-data: FAIL%s — mktemp\n' "$RED" "$RESET"
58+
exit 2
59+
}
60+
trap 'rm -rf "$TMP"' EXIT INT TERM HUP
61+
4862
fail=0
4963
nd=0
5064

@@ -133,21 +147,20 @@ if [ -f "$SVG" ]; then
133147
while IFS="$(printf '\t')" read -r arch ver; do
134148
grep -q ">$arch<" "$SVG" || { echo "MISSARCH $arch"; continue; }
135149
grep -q ">$ver<" "$SVG" || echo "MISSVER $arch $ver"
136-
done >/tmp/.kd.$$ 2>/dev/null
150+
done >"$TMP/kd" 2>/dev/null
137151
while read -r kind rest; do
138152
case $kind in
139153
MISSARCH) printf '%s history-table arch missing from the timeline%s: %s\n' "$RED" "$RESET" "$rest"; fail=1 ;;
140154
MISSVER) printf '%s timeline version disagrees with the history table%s: %s\n' "$RED" "$RESET" "$rest"; fail=1 ;;
141155
esac
142-
done </tmp/.kd.$$
143-
rm -f /tmp/.kd.$$
156+
done <"$TMP/kd"
144157
fi
145158

146159
# ---- vmsplit.svg vs the vmsplit table ------------------------------------
147160
SVG=docs/diagrams/vmsplit.svg
148161
if [ -f "$SVG" ]; then
149162
awk -F'|' '/^\| [0-9]G/ { gsub(/^ +| +$/,"",$2); gsub(/^ +| +`|` +$/,"",$3); print $2 "\t" $3 }' \
150-
"$KAS" >/tmp/.vs.$$ 2>/dev/null
163+
"$KAS" >"$TMP/vs" 2>/dev/null
151164
while IFS="$(printf '\t')" read -r name po; do
152165
[ -n "$name" ] || continue
153166
grep -q ">$name<" "$SVG" || {
@@ -158,8 +171,7 @@ if [ -f "$SVG" ]; then
158171
printf '%s vmsplit PAGE_OFFSET disagrees with the table%s: %s (%s)\n' "$RED" "$RESET" "$name" "$po"
159172
fail=1
160173
}
161-
done </tmp/.vs.$$
162-
rm -f /tmp/.vs.$$
174+
done <"$TMP/vs"
163175
fi
164176

165177
if [ "$fail" -eq 0 ]; then

0 commit comments

Comments
 (0)