NewVuln67 Sequence nextval Skips Minimum Value at INT64_MIN Boundary
What happens?
A descending sequence created with NO MINVALUE (or MINVALUE -9223372036854775808) has its
minimum automatically set to INT64_MIN (-9223372036854775808). Due to an off-by-one in the
overflow guard in SequenceCatalogEntry::NextValue, the call that would return INT64_MIN instead
throws an exception, silently skipping the minimum value.
The logic in src/catalog/catalog_entry/sequence_catalog_entry.cpp (lines 58-70) is
result = data.counter;
bool overflow = !TryAddOperator::Operation(data.counter, data.increment, next_counter);
// ...
if (result < data.min_value || (overflow && data.increment < 0)) {
throw SequenceException("nextval: reached minimum value ...");
}When data.counter == INT64_MIN
result = INT64_MIN(a valid value within[min_value, max_value])next_counter = INT64_MIN + (-1)overflows →overflow = trueresult < data.min_valueevaluates toINT64_MIN < INT64_MIN→ falseoverflow && data.increment < 0evaluates totrue && true→ throws immediately
The throw fires one call too early. INT64_MIN is never returned. The sequence effectively has one
fewer value than its defined range.
To Reproduce
CREATE SEQUENCE s INCREMENT BY -1 NO MINVALUE START -9223372036854775807;
SELECT nextval('s');
-- Expected: -9223372036854775807 (OK, first valid value)
SELECT nextval('s');
-- Expected: -9223372036854775808 (should return min value)
-- Actual: Sequence Error: nextval: reached minimum value of sequence "s" (-9223372036854775808)Relevant code locations
| File | Line | Note |
|---|---|---|
src/catalog/catalog_entry/sequence_catalog_entry.cpp |
58-70 | NextValue overflow guard fires one call too early |
src/parser/peg/transformer/transform_create_sequence.cpp |
55-56 | Default min_value set to NumericLimits<int64_t>::Minimum() for NO MINVALUE |
OS:
Ubuntu 24.04.2 LTS, x86_64 (Linux 6.8.0-55-generic)
DuckDB Version:
v2.0.0-dev85809 (Development Version) d8a1bd4f4f
DuckDB Client:
CLI (Command Line Interface), release build (make release)
Hardware:
No response
Full Name:
Yuelin
Affiliation:
Individual
Did you include all relevant configuration (e.g., CPU architecture, Linux distribution) to reproduce the issue?
- Yes, I have
Did you include all code required to reproduce the issue?
- Yes, I have
Did you include all relevant data sets for reproducing the issue?
Yes
Source: duckdb/duckdb