Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 06fe000

Browse files
author
Max Schaefer
committed
Conservatively handle indirect updates through pointer-type receiver.
Method references `x.m` where the receiver of `m` is a pointer implicitly take the address of `x`, so they should be treated much the same as `&x` in terms of data flow. (Ideally we'd make this explicit in the data-flow graph itself, but that's for another PR.)
1 parent d14eb85 commit 06fe000

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

ql/src/semmle/go/dataflow/SSA.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class SsaSourceVariable extends LocalVariable {
2424
// variables that have their address taken
2525
exists(AddressExpr addr | addr.getOperand().stripParens() = getAUse())
2626
or
27+
exists(DataFlow::MethodReadNode mrn |
28+
mrn.getReceiver() = getARead() and
29+
mrn.getMethod().getReceiverType() instanceof PointerType
30+
)
31+
or
2732
// variables where there is an unresolved reference with the same name in the same
2833
// scope or a nested scope, suggesting that name resolution information may be incomplete
2934
exists(FunctionScope scope, FuncDef inner |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
| CompareIdenticalValues.go:9:3:9:8 | ...<=... | This expression compares $@ to itself. | CompareIdenticalValues.go:9:3:9:3 | y | an expression |
22
| tst.go:6:9:6:14 | ...==... | This expression compares $@ to itself. | tst.go:6:9:6:9 | x | an expression |
3+
| tst.go:60:9:60:14 | ...==... | This expression compares $@ to itself. | tst.go:60:9:60:9 | y | an expression |
34
| vp.go:16:9:16:38 | ...!=... | This expression compares $@ to itself. | vp.go:16:9:16:21 | call to GetLength | an expression |

ql/test/query-tests/RedundantCode/CompareIdenticalValues/tst.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,25 @@ func baz() bool {
3737
bump(&x)
3838
return x == 0
3939
}
40+
41+
type counter int
42+
43+
func (x *counter) bump() {
44+
*x++
45+
}
46+
47+
func (x counter) bimp() {
48+
x++
49+
}
50+
51+
func baz2() bool {
52+
var x counter
53+
x.bump()
54+
return x == 0 // OK
55+
}
56+
57+
func baz3() bool {
58+
var y counter
59+
y.bimp()
60+
return y == 0 // NOT OK
61+
}

0 commit comments

Comments
 (0)