Skip to content

Fix panic on negative slice indices that underflow after adjustment#2646

Merged
mikefarah merged 1 commit intomikefarah:masterfrom
jandubois:fix-slice-negative-index
Apr 6, 2026
Merged

Fix panic on negative slice indices that underflow after adjustment#2646
mikefarah merged 1 commit intomikefarah:masterfrom
jandubois:fix-slice-negative-index

Conversation

@jandubois
Copy link
Copy Markdown
Contributor

sliceArrayOperator adjusts negative indices by adding Content length, but does not clamp the result. When the absolute value of a negative index exceeds Content length (e.g. .[-99999:3] on a 3-element array), the adjusted index remains negative and causes an out-of-bounds access in the Content slice loop.

Extract the adjust-and-clamp logic into clampSliceIndex and use it for both index positions.

Reproducer (panics before this fix, returns full array after):

echo '[a, b, c]' | yq '.[-99999:3]'

Found by OSS-Fuzz via the lima project's FuzzEvaluateExpression target. https://issues.oss-fuzz.com/issues/438776028

sliceArrayOperator adjusts negative indices by adding Content length,
but does not clamp the result. When the absolute value of a negative
index exceeds Content length (e.g. .[-99999:3] on a 3-element array),
the adjusted index remains negative and causes an out-of-bounds access
in the Content slice loop.

Extract the adjust-and-clamp logic into clampSliceIndex and use it for
both index positions.

Reproducer (panics before this fix, returns full array after):

    echo '[a, b, c]' | yq '.[-99999:3]'

Found by OSS-Fuzz via the lima project's FuzzEvaluateExpression target.
https://issues.oss-fuzz.com/issues/438776028

Signed-off-by: Jan Dubois <jan@jandubois.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mikefarah
Copy link
Copy Markdown
Owner

Love your work @jandubois

@mikefarah mikefarah merged commit 80139ae into mikefarah:master Apr 6, 2026
3 checks passed
@jandubois jandubois deleted the fix-slice-negative-index branch April 6, 2026 17:00
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