Skip to content

Saturate runtime cost accumulation to prevent overflow - #1449

Closed
TristonianJones wants to merge 1 commit into
cel-expr:masterfrom
TristonianJones:cost-saturation-fix
Closed

Saturate runtime cost accumulation to prevent overflow#1449
TristonianJones wants to merge 1 commit into
cel-expr:masterfrom
TristonianJones:cost-saturation-fix

Conversation

@TristonianJones

Copy link
Copy Markdown
Collaborator

CostTracker.Observe accumulated call costs with an unchecked +=, so the code was switched to cost.SafeAdd
to preserve saturation.

CostTracker.Observe accumulated call costs with an unchecked +=, so an
overload reporting an unbounded actual cost wrapped the running total back
to a small value whenever any cost had already accrued. json.encode, which
reports math.MaxUint64, evaluated to a cost of 0 for json.encode(v) and 1
for json.encode(v) == json.encode(v). Only the all-constant case, where
nothing has accrued yet, produced the intended cost.

A wrapped total silently defeats CostTrackerLimit: json.encode(v) evaluated
without error under a limit of 1000.

Accumulate with cost.SafeAdd instead, matching the saturating convention
documented throughout the cost package. The qualifier, ident and constructor
increments are saturated as well, since those otherwise wrap an already
saturated total back down to zero.

TestEncodersCosts/json_encode_dyn asserted an unbounded estimated cost
alongside an actual cost of 1, which was the wrapped value; it now expects
math.MaxUint64.
@TristonianJones
TristonianJones requested review from jnthntatum and removed request for jnthntatum August 28, 2026 22:46
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.

1 participant