Skip to content

Commit 97d089f

Browse files
authored
Merge pull request #22670 from owen-mc/go/fix/impl-field-cfg
Go: fix CFG bug - implicit field selections with depth 0
2 parents 4841ad5 + 596df99 commit 97d089f

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ module CfgImpl {
682682
e.(Go::PromotedSelector).refersTo(explicitField) and
683683
baseType.getFieldAtDepth(_, explicitFieldDepth) = explicitField
684684
|
685-
index = explicitFieldDepth - implicitFieldDepth
685+
index = explicitFieldDepth - implicitFieldDepth and index > 0
686686
)
687687
)
688688
or
@@ -692,7 +692,8 @@ module CfgImpl {
692692
baseType = e.(Go::PromotedSelector).getSelectedStructType() and
693693
e.(Go::PromotedSelector).refersTo(method) and
694694
baseType.getMethodAtDepth(_, mDepth) = method and
695-
index = mDepth - implicitFieldDepth
695+
index = mDepth - implicitFieldDepth and
696+
index > 0
696697
|
697698
method = baseType.getMethodOfEmbedded(implicitField, _, implicitFieldDepth + 1)
698699
or

‎go/ql/test/query-tests/RedundantCode/UnreachableStatement/main.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,21 @@ func test23() {
186186
unreachable() // OK: deliberately unreachable
187187
}
188188

189+
type (
190+
embedded struct{}
191+
recursive struct {
192+
*embedded
193+
value int
194+
*recursive
195+
}
196+
)
197+
198+
func test24(x *recursive) []int {
199+
values := []int{0}
200+
if x.value != 0 {
201+
values = append(values, x.value)
202+
}
203+
return values // OK: reachable because value is a direct field of recursive
204+
}
205+
189206
func main() {}

0 commit comments

Comments
 (0)