Saturate runtime cost accumulation to prevent overflow - #1449
Closed
TristonianJones wants to merge 1 commit into
Closed
Saturate runtime cost accumulation to prevent overflow#1449TristonianJones wants to merge 1 commit into
TristonianJones wants to merge 1 commit into
Conversation
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
requested review from
jnthntatum
and removed request for
jnthntatum
August 28, 2026 22:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CostTracker.Observeaccumulated call costs with an unchecked +=, so the code was switched tocost.SafeAddto preserve saturation.