fix: guard make() capacity against integer overflow#401
Merged
Conversation
CodeQL's go/allocation-size-overflow flagged several make() capacity hints that add a small constant to, or double, the length of an in-memory collection, where it cannot prove the size arithmetic will not overflow. Those lengths are bounded by available memory so the overflow is unreachable in practice, but the arithmetic is now guarded so it is provably safe and the alerts no longer fire.
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.
CodeQL's
go/allocation-size-overflowquery opened five high-severity alerts (3-7) againstmake()calls whose capacity hint adds a small constant to, or doubles, the length of an in-memory collection - patterns likelen(base)+4andlen(pairs)*2. Because those lengths are bounded by available memory they cannot approachmath.MaxInt, so the overflow is unreachable in practice, but CodeQL cannot prove that and the warnings recur on every scan.Each capacity computation now goes through a small
safeCaphelper that returns the sum unless it would overflow, in which case it falls back to the base length, so the hint is unchanged for every realistic input while the arithmetic is provably non-overflowing. The same idiom intasks/format.gothat CodeQL had not yet flagged is folded in so the treatment stays consistent.Resolves code scanning alerts 3-7.