Styxis
Company
Knowledge

DuckDB integer overflow: do not rely on WHERE to protect multiplication

Success that depends on filter evaluation order is not evidence that arithmetic inputs are safe.

Establish safety before the filter

Do not assume filter evaluation order will protect unsafe arithmetic. A literal and a scalar subquery returning the same value can take different execution paths. When connecting Truthound, an explicit input-range check is clearer than interpreting a successful SQL execution as proof of safety. [1, 2, 3]

The same value takes a different path

The DuckDB report replaces a literal with a scalar subquery returning the same integer. A query that returned an empty result then fails with multiplication overflow. Evaluating the multiplication would exceed the range in either form. The successful path therefore does not establish that the arithmetic is valid. [1]

Specify every intermediate type

Cast to a sufficiently wide type before multiplication and check the allowed input range for each arithmetic stage. Do not use optimizer-dependent success as safety evidence. A safe initial square does not establish a safe subsequent product; record intermediate types and bounds for the complete expression. [1, 3]

An illustrative bound for the square only

For the INT32 square stage alone, an absolute input of 46340 is the boundary used here. The input 46340 satisfies this precondition, while 50000 does not. This is not a safe-input bound for an entire multi-factor expression. Negative inputs must be handled through their magnitudes as well. [1, 3]

What the range check establishes

Truthound Core's BetweenValidator can inspect the upper bound of prepared input magnitudes. Compute those magnitudes in a wide enough representation to avoid overflow at the minimum signed value itself. The check does not guarantee SQL evaluation order. Even a wider arithmetic path needs a separate check when exporting into the final storage type. [1, 2, 3]

Application conditions

Link source integers and magnitudes calculated in a sufficiently wide type to the same square-operation stage, with a separately approved bound.

Alternatives

Perform multiplication in a wider integer type and calculate bounds separately for every stage.

Limitations

  • Truthound does not guarantee SQL evaluation order. This square-stage bound is not the safe range for the report’s complete three-factor product.

Evidence

Event checked
Subject checked
Modified