fix: std.format rejects boolean for numeric conversion codes#1016
Merged
stephenamar-db merged 2 commits intoJun 24, 2026
Conversation
Motivation: sjsonnet silently coerced booleans to integers for numeric format codes (%d, %f, %e, %x, %o, %g etc.), treating true as 1 and false as 0. Both C++ jsonnet and jrsonnet correctly reject this with a type error. This is a permissiveness bug that can mask logic errors in Jsonnet programs. Modification: - Format.scala: Restrict Val.True/Val.False match arms to only allow %s conversion. All numeric conversion codes (%d, %i, %u, %o, %x, %X, %e, %E, %f, %F, %g, %G) now produce "expected number or string, got boolean". - Added error tests for %d and %f with boolean values. Result: "%d" % true now errors instead of silently returning "1". Cross-implementation comparison: | Expression | C++ jsonnet | go-jsonnet | jrsonnet | sjsonnet (before) | sjsonnet (after) | |-----------------|---------------------|------------|-----------------------|--------------------|------------------| | "%d" % true | ERROR: got boolean | ERROR | ERROR: got boolean | "1" ❌ | ERROR ✅ | | "%f" % false | ERROR | ERROR | ERROR | "0.000000" ❌ | ERROR ✅ | | "%x" % true | ERROR | ERROR | ERROR | "1" ❌ | ERROR ✅ | | "%e" % true | ERROR | ERROR | ERROR | "1.000000e+00" ❌ | ERROR ✅ | | "%s" % true | "true" | "true" | "true" | "true" ✅ | "true" ✅ |
He-Pin
commented
Jun 24, 2026
| case 'E' => formatExponent(formatted, b) | ||
| case 'f' | 'F' => formatFloat(formatted, b) | ||
| case 'g' => formatGeneric(formatted, b).toLowerCase | ||
| case 'G' => formatGeneric(formatted, b) |
stephenamar-db
approved these changes
Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: std.format rejects boolean for numeric conversion codes
Motivation
std.formatsilently coerced booleans to integers for all numeric format codes (%d,%f,%e,%x,%o,%getc.), treatingtrueas 1 andfalseas 0. C++ jsonnet, go-jsonnet, and jrsonnet all correctly reject this with a type error.Modification
Format.scala: RestrictVal.True/Val.Falsematch arms to only allow%sconversion. All numeric codes now produce"expected number or string, got boolean".Result
"%d" % trueand similar expressions now correctly error instead of silently coercing."%d" % true"1"❌"%f" % false"0.000000"❌"%x" % true"1"❌"%s" % true"true""true""true""true"✅"true"✅