LIMIT plus OFFSET overflows and can panic on empty results

perfloop-oss/vitess · Numeric Representation Loss

https://perfloop.ai/t/oss/case_9sk9aty3b8

Verdict

VERIFIED · settled 2026-11-16 · pull request opened as vitessio/vitess#22083

What happened: The check fails before this change and passes with it.

Hypothesis

VTGate checks the LIMIT count and OFFSET separately, then adds them as machine integers. If the true sum is larger than the largest accepted value, it wraps around. A unary query can then panic while trimming an empty result, and streaming can send a negative row bound to the child query. The SQL-level route and how often such values occur are still unmeasured.

In `Limit.TryExecute`, `getCountAndOffset` uses `getIntFrom`, which accepts each value from 0 through the machine-int maximum. `TryExecute` then writes `__upper_limit` as `int64(count + offset)` and uses `count+offset` to slice `result.Rows`. `TryStreamExecute` builds the same child bound from the same sum. The planner can route this value to child routes through `:__upper_limit` when a pushed `LIMIT ... OFFSET` is not a literal.

A bounded check ran here as a temporary `go/vt/vtgate/engine` `_test.go` file, which the probe removed afterward. It mapped Count and Offset to [0, MaxInt] and capped child results at 8 rows. Seeds covered {0, 1, 2, MaxInt-1, MaxInt} squared, 0 to 2 child rows, and both execution modes.

The predicate allowed an explicit combined-range error but rejected panics and negative child bounds. `go test ./go/vt/vtgate/engine -run '^$' -fuzz '^FuzzHuntLimitCombinedRange$' -fuzztime=10s -parallel=1 -timeout=60s` failed on seed 54: `c=1 o=9223372036854775807 child_rows=0 stream=false: panic=runtime error: slice bounds out of range [:-9223372036854775808]`.

I reduced it to one deterministic test with exactly Count=1, Offset=MaxInt and an empty successful child. `go test ./go/vt/vtgate/engine -run '^TestHuntLimitCombinedRangeEmptyCounterexample$' -count=1 -v -timeout=60s` panicked at `limit.go:74` in unary mode. In streaming mode it reported `negative pushed upper_limit=-9223372036854775808`.

Upstream check: the bounded GitHub open-PR scan found no LIMIT-owner change. Vitess PR #22080 (https://github.com/vitessio/vitess/pull/22080) fixes evaluator multiplication overflow, not this engine addition. Prior repair hyp_2vd6m9hj73 changes eager unary shard collection, not this range defect. Recent upstream history for the owner files was not available: the checkout is shallow and history requests were refused.

The smallest Case proof is to keep a failing-main regression and run it on the repair. Use the Count=1/Offset=MaxInt empty input, both modes, and small nonempty controls. Confirm that individual conversion errors and existing slicing behavior stay unchanged. A Case should also decide whether an end-to-end normalized SQL query reaches this engine boundary.

Change to test: Make unary and streaming LIMIT execution derive the child upper bound and unary slice bound through one checked combined-range calculation. Give out-of-range sums a deterministic error, or use an equivalent valid representation, instead of wrapping.

Where it lives

perfloop-oss/vitess

Evidence

The check fails before this change and passes with it: `Limit with Count=1 and Offset=math.MaxInt64 over an empty successful input returns no rows and no error from TryExecute (without panicking) and from TryStreamExecute, and passes __upper_limit=9223372036854775807 (not a wrapped negative value) to its input in both modes.`

The check fails before this change and passes with it: `Limit with Count=math.MaxInt64 and Offset=1 over a three-row input returns the last two rows and no error from TryExecute (without panicking) and from TryStreamExecute, and passes __upper_limit=9223372036854775807 (not a wrapped negative value) to its input in both modes.`

The check fails before this change and passes with it: `Limit with an unsigned Count of 18446744073709551615 and Offset=1 over a three-row input returns the last two rows and no error (instead of 'requested limit is out of range') from TryExecute and TryStreamExecute, and passes __upper_limit=9223372036854775807 to its input in both modes.`

The check fails before this change and passes with it: `Limit with Count=1 and an unsigned Offset of 18446744073709551615 over a three-row input returns no rows and no error (instead of 'requested limit is out of range') from TryExecute and TryStreamExecute, and passes __upper_limit=9223372036854775807 to its input in both modes.`

The check fails before this change and passes with it: `MemorySort with an unsigned UpperLimit literal of 18446744073709551615 returns every input row in sorted order and no error (instead of 'requested limit is out of range') from TryExecute, while its existing sort and Int64 upper-limit results stay as before.`

The check fails before this change and passes with it: `The planner, given the unnormalized query 'select id from user limit 1 offset 9223372036854775807' on the sharded user keyspace, returns a plan instead of a VT13001 error: a Limit with Count 1 and Offset 9223372036854775807 above a scatter Route whose pushed-down query limits each shard to 9223372036854775807 rows.`

The check fails before this change and passes with it: `The planner, given the unnormalized query 'select id from user limit 18446744073709551615 offset 1' on the sharded user keyspace, returns a plan instead of a VT13001 error: a Limit with Count 18446744073709551615 and Offset 1 above a scatter Route whose pushed-down query limits each shard to 9223372036854775807 rows.`

The check fails before this change and passes with it: `The planner, given the unnormalized query 'select id from (select id from user limit 0 offset 9223372036854775807) as t limit 1 offset 1' on the sharded user keyspace, returns a plan without panicking: the merged Limit has Count 1 and Offset 9223372036854775807 (the summed offsets saturated, not a wrapped negative value) above a scatter Route whose pushed-down query limits each shard to 9223372036854775807 rows.`

The check fails before this change and passes with it: `The VTGate executor runs 'select col1 from user limit 18446744073709551615 offset 1' against an 8-shard scatter route, with query normalization off and on, through both Execute and StreamExecute, without error: it returns 7 of the 8 shard rows and sends each shard one query limited to 9223372036854775807 rows (a literal without normalization, :__upper_limit with normalization) with __upper_limit=9223372036854775807.`

Timeline