Skip to content

fix: add bounds check for star width/precision in std.format - #1090

Merged
stephenamar-db merged 1 commit into
databricks:masterfrom
He-Pin:fix/format-star-spec-bounds-check
Jul 28, 2026
Merged

fix: add bounds check for star width/precision in std.format#1090
stephenamar-db merged 1 commit into
databricks:masterfrom
He-Pin:fix/format-star-spec-bounds-check

Conversation

@He-Pin

@He-Pin He-Pin commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Motivation

std.format("%*d", [5]) threw ArrayIndexOutOfBoundsException instead of a clean error. The bounds check at the top of the loop ran before star consumption but not after — once * width/precision consumed values and incremented i, the subsequent valuesArr.value(i) access had no guard.

Both go-jsonnet and jrsonnet report a clean "not enough values" error. Python raises TypeError: not enough arguments for format string.

Modification

Add i >= valuesArr.length guards after each i += 1 in the three star-consuming branches: (true,false), (false,true), and (true,true). The error message matches the existing pre-loop check format.

Result

Star format specs with insufficient values now produce a clean user-facing error instead of an internal crash.

Behavior comparison

Input go-jsonnet jrsonnet Python sjsonnet (before) sjsonnet (after)
"%*d" % [5] error: "Not enough values to format: 1, expected more than 1" error: "not enough values" TypeError: not enough arguments AIOOBE crash error: "too few values to format: 1, expected at least 2"
"%*.*d" % [5, 3] error: "Not enough values to format: 2, expected more than 2" error: "not enough values" TypeError: not enough arguments AIOOBE crash error: "too few values to format: 2, expected at least 3"
"%*d" % [5, 42] " 42" " 42" " 42" " 42" " 42" (unchanged)
"%*s" % [] error: "Not enough values" error: "not enough values" TypeError error: "too few values to format: 0, expected at least 1" (unchanged, caught by pre-loop check)

Test plan

  • std.format("%*d", [5]) → clean error instead of AIOOBE
  • std.format("%*.*d", [5, 3]) → clean error instead of AIOOBE
  • std.format("%*d", [5, 42])" 42" (no regression)
  • Full test suite passes (73/73)

Motivation:
`std.format("%*d", [5])` threw ArrayIndexOutOfBoundsException instead of
a clean error. The pre-loop bounds check ran before star consumption but
not after — once `*` width/precision consumed values and incremented `i`,
the subsequent `valuesArr.value(i)` had no guard. go-jsonnet and jrsonnet
both report "not enough values" cleanly; Python raises TypeError.

Modification:
Add `i >= valuesArr.length` guards after each `i += 1` in the three
star-consuming branches: (true,false), (false,true), and (true,true).
Error message matches the existing pre-loop check format.

Result:
Star format specs with insufficient values produce a clean user-facing
error ("too few values to format: N, expected at least M") instead of an
internal ArrayIndexOutOfBoundsException crash.
@He-Pin
He-Pin force-pushed the fix/format-star-spec-bounds-check branch from 66d49cc to 5446295 Compare July 27, 2026 19:04
@stephenamar-db
stephenamar-db merged commit ea45dba into databricks:master Jul 28, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants