Skip to content

Conversation

@thevilledev
Copy link
Contributor

Motivation

OSS-Fuzz discovered a panic in #910 when the sortBy function receives a non-string value as its third (order) argument. The panic occurred because the VM performed a type assertion vm.pop().(string) without verifying the type first. The documentation shows the order argument accepts "asc" or "desc", but there was no validation preventing other types from being passed.

Changes

The type checker now validates the sortBy's third argument is a string type, producing a compile-time error. For dynamic cases where type information isn't available at compile time, the VM and builtin sort function now use type assertions that return errors instead of panicking. A regression test reproduces the exact fuzz scenario.

Further comments

This PR also removes three expressions from the generated test cases. These passed non-string values (integers and arrays) as the order argument. These appeared to work because the optimiser folded the patterns like x && false to false at compile time. Meaning the sortBy calls were never executed.

If I understood the test corpus & the generator correctly, these test cases won't end up re-generated as they are now omitted as invalid ones.

The sortBy and sort functions accept an optional order argument that
must be "asc" or "desc". Previously, passing a non-string value would
cause a panic due to an unsafe type assertion in the VM. This adds
compile-time validation in the type checker and defensive runtime
checks in both the VM and builtin implementations to return proper
errors instead of panicking.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
@thevilledev thevilledev marked this pull request as ready for review January 23, 2026 19:52
@antonmedv antonmedv merged commit 77df211 into expr-lang:master Jan 23, 2026
18 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