Skip to content

Commit 18a1075

Browse files
authored
Merge pull request #20523 from smowton/smowton/fix/mistyped-exp-fp
Go: mistyped-exponentiation: notice constants with likely-bitmask values
2 parents a0b533b + f5f6119 commit 18a1075

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

go/ql/src/InconsistentCode/MistypedExponentiation.ql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313

1414
import go
1515

16+
private Expr getConstantInitialiser(Expr e) {
17+
exists(DeclaredConstant c | e = c.getAReference() | result = c.getInit())
18+
}
19+
1620
/** Holds if `e` is not 0 and is either an octal or hexadecimal literal, or the number one. */
1721
predicate maybeXorBitPattern(Expr e) {
1822
// 0 makes no sense as an xor bit pattern
1923
not e.getNumericValue() = 0 and
2024
// include octal and hex literals
21-
e.(IntLit).getText().matches("0%")
25+
[e, getConstantInitialiser(e)].(IntLit).getText().matches("0%")
2226
or
2327
e.getNumericValue() = 1
2428
}

go/ql/test/query-tests/InconsistentCode/MistypedExponentiation/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ func main() {
2222

2323
mask := (((1 << 10) - 1) ^ 7) // OK
2424

25+
const (
26+
c1 = 0x1234
27+
c2 = 0x5678
28+
)
29+
30+
fmt.Println(c1 ^ c2) // OK
31+
2532
// This is not ok, but isn't detected because the multiplication binds tighter
2633
// than the xor operator and so the query doesn't see a constant on the left
2734
// hand side of ^.

0 commit comments

Comments
 (0)