Skip to content

fix(G115): bound the results of math/bits bit counts - #1850

Open
ma7bor wants to merge 1 commit into
securego:masterfrom
ma7bor:g115-math-bits
Open

ma7bor wants to merge 1 commit into
securego:masterfrom
ma7bor:g115-math-bits

Conversation

@ma7bor

@ma7bor ma7bor commented Nov 29, 2026

Copy link
Copy Markdown

G115 reports converting the result of a math/bits bit count to a small integer type, although these functions can't return more than the width of their operand:

func width(x uint64) uint8 {
	return uint8(bits.Len64(x)) // G115: integer overflow conversion int -> uint8
}

Len, LeadingZeros, TrailingZeros and OnesCount return an int between 0 and 8, 16, 32 or 64, and their unsuffixed forms take a uint, which has at most 64 bits. So the result fits even in int8 and uint8. On master all eight variants I tried (Len64, Len32, Len, TrailingZeros64, LeadingZeros32, OnesCount64, OnesCount8, and int8(bits.Len64(x))) are reported.

The range analysis now gives these calls the range 0 to their width in ComputeRange, as it does for the min and max builtins, and treats them as non-negative in isNonNegativeRecursive. The match is on the package path and the function name, so other math/bits functions are unchanged, and so is arithmetic on a bit count: uint8(bits.Len64(x) * 8) and uint8(bits.Reverse64(x)) are still reported.

I added both to testutils/g115_samples.go: a sample with five conversions of bit counts that expects no issue, and one with the two conversions above that expects two. Without the change, the first sample fails with five G115 issues. With it, go test ./analyzers/... passes, and go vet and gofmt are clean, on Go 1.27.4. I didn't run golangci-lint locally.

@ma7bor
ma7bor deployed to security-review November 29, 2026 11:45 — with GitHub Actions Active
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.77%. Comparing base (244f1f6) to head (3bf951b).
⚠️ Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
analyzers/range_analyzer.go 90.47% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1850      +/-   ##
==========================================
- Coverage   85.80%   85.77%   -0.04%     
==========================================
  Files         111      111              
  Lines        9581     9602      +21     
==========================================
+ Hits         8221     8236      +15     
- Misses       1360     1366       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ccojocar

Copy link
Copy Markdown
Member

Can you please rebase and fix the conflicts? Thanks

LeadingZeros, TrailingZeros, OnesCount and Len in math/bits return at
most the width of their operand, so converting their result to any
integer type cannot overflow. The range analysis didn't know these
calls and reported every such conversion.
@ma7bor
ma7bor deployed to security-review November 30, 2026 17:19 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
security-review — 1171130b Deployed Nov 30, 2026 by ma7bor via barry-ai-security-review #2911
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.

2 participants