Skip to content

Commit 14cffc3

Browse files
authored
Merge pull request github#15128 from owen-mc/go/fix-fp-incorrect-integer-conversion-signedness
Go: fix FP in incorrect integer conversion query relating to strict comparisons with MaxInt and MaxUint
2 parents 466536a + 0279e49 commit 14cffc3

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

go/ql/lib/semmle/go/security/IncorrectIntegerConversionLib.qll

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ abstract private class MaxIntOrMaxUint extends DeclaredConstant {
1515
*/
1616
predicate isBoundFor(int b, int architectureBitSize, float strictnessOffset) {
1717
// 2.pow(x) - 1 - strictnessOffset <= 2.pow(b) - 1
18-
exists(int x |
19-
x = this.getOrder(architectureBitSize) and
20-
b = validBitSize() and
21-
(
22-
strictnessOffset = 0 and x <= b
23-
or
24-
strictnessOffset = 1 and x <= b - 1
25-
)
26-
)
18+
// For the values that we are restricting `b` to, `strictnessOffset` has no
19+
// effect on the result, so we can ignore it.
20+
b = validBitSize() and
21+
strictnessOffset = [0, 1] and
22+
this.getOrder(architectureBitSize) <= b
2723
}
2824
}
2925

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* There was a bug in the query `go/incorrect-integer-conversion` which meant that upper bound checks using a strict inequality (`<`) and comparing against `math.MaxInt` or `math.MaxUint` were not considered correctly, which led to false positives. This has now been fixed.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
failures
21
testFailures
2+
failures

go/ql/test/query-tests/Security/CWE-681/IncorrectIntegerConversion.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,10 @@ func typeAssertion(s string) {
491491
}
492492

493493
}
494+
495+
func dealWithArchSizeCorrectly(s string) uint {
496+
if i, err := strconv.ParseUint(s, 10, 64); err == nil && i < math.MaxUint {
497+
return uint(i)
498+
}
499+
return 0
500+
}

0 commit comments

Comments
 (0)