Skip to content

fix: reject negative indent instead of panicking#2746

Open
StressTestor wants to merge 1 commit into
mikefarah:masterfrom
StressTestor:fix/2329-negative-indent-validation
Open

fix: reject negative indent instead of panicking#2746
StressTestor wants to merge 1 commit into
mikefarah:masterfrom
StressTestor:fix/2329-negative-indent-validation

Conversation

@StressTestor

@StressTestor StressTestor commented Jun 12, 2026

Copy link
Copy Markdown

Generated by an AI agent (Claude Code) acting on the user's behalf, not the user personally.

Fixes #2329.

the problem

a negative indent crashes yq instead of erroring cleanly:

$ echo 'hello: world' | yq -I=-1 '.'
panic: go-yaml dump error in serializer: cannot indent to a negative number of spaces
[stack trace...]

the -I/--indent flag is an int with no lower bound, so a negative value flows straight through to the yaml encoder's SetIndent, which panics. a bad flag value should be a clean error, not a stack trace.

the fix

reject a negative indent in validateCommandFlags (the same place the other flag-combo checks live), before any encoding happens:

if indent < 0 {
    return fmt.Errorf("indent must not be negative")
}

after:

$ echo 'hello: world' | yq -I=-1 '.'
Error: indent must not be negative      # exit 1, no panic

-I=0 stays valid (it's documented for one-line json), so the guard is < 0, not <= 0. validating at the flag layer covers every output format at once rather than patching each encoder.

tests

extended TestValidateCommandFlags with a negative case (fails without the guard, passes with it) and a zero case (pins that -I=0 stays valid). existing cases unaffected.

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.

Indent parameter validation

1 participant