diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-17 08:15:35 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-17 08:15:35 +0300 |
| commit | 50d951ab3bb28861b34bc190911dd76bfb93e71c (patch) | |
| tree | d78bfe5a1620502fca10e78fc0328092d408742e /src | |
| parent | ca5bcf37e40d4dbd41dc9d9d001d6f4b9c6dcd65 (diff) | |
bn0 guard preview_num arithmetic against non-numeric input
The preview_num next/prev render handlers computed neighbour page numbers
with $(( context_value +/- 1 )) but only guarded against an empty value.
A non-numeric preview_num context value (e.g. a stray string) slipped past
the [ -n ] check and triggered a bash arithmetic syntax error which, under
set -e, aborted the whole script.
Validate the context value is a non-negative integer
([[ value =~ ^[0-9]+$ ]]) before the arithmetic in both
prepare_template_render_var__preview_num_next_html and __preview_num_prev_html.
Invalid or missing values now default to an empty render value, matching the
existing missing-neighbour behaviour; the valid-numeric path is unchanged.
Add test_template_render_var_preview_num_guards_non_numeric in tests/cli.sh,
which drives the handlers directly under bash -euo pipefail to prove a bad
preview_num no longer crashes and a numeric one still yields the exact
+1 / -1 neighbour.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/template.source.sh | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/lib/template.source.sh b/src/lib/template.source.sh index cd44a45..cfa826e 100644 --- a/src/lib/template.source.sh +++ b/src/lib/template.source.sh @@ -582,7 +582,12 @@ prepare_template_render_var__preview_num_next_html() { local context_value template_context_value_to context_value "$context_name" "$source_name" - if [ -n "$context_value" ]; then + # Guard the arithmetic: only a non-negative integer is safe to feed to + # $(( )). An empty or non-numeric context value (e.g. a stray string) would + # otherwise throw a bash arithmetic syntax error and, under set -e, abort the + # whole script. Treat any invalid/missing preview_num as "no neighbour" and + # emit an empty render value, matching the missing-value behaviour. + if [[ "$context_value" =~ ^[0-9]+$ ]]; then html_escape_to out_ref "$(( context_value + 1 ))" else out_ref='' @@ -598,7 +603,12 @@ prepare_template_render_var__preview_num_prev_html() { local context_value template_context_value_to context_value "$context_name" "$source_name" - if [ -n "$context_value" ]; then + # Guard the arithmetic: only a non-negative integer is safe to feed to + # $(( )). An empty or non-numeric context value would otherwise throw a bash + # arithmetic syntax error and, under set -e, abort the whole script. Treat + # any invalid/missing preview_num as "no neighbour" and emit an empty render + # value, mirroring the next-html handler. + if [[ "$context_value" =~ ^[0-9]+$ ]]; then html_escape_to out_ref "$(( context_value - 1 ))" else out_ref='' |
