Skip to content

Commit c8d98ae

Browse files
Marcono1234smowton
authored andcommitted
Java: Fix Field.getInitializer() matching non-initializer assignments
1 parent c66a34b commit c8d98ae

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

java/ql/lib/semmle/code/java/Member.qll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,13 @@ class Field extends Member, ExprParent, @field, Variable {
600600

601601
/** Gets the initializer expression of this field, if any. */
602602
override Expr getInitializer() {
603-
exists(AssignExpr e, InitializerMethod im |
603+
exists(AssignExpr e, InitializerMethod im, ExprStmt exprStmt |
604604
e.getDest() = this.getAnAccess() and
605605
e.getSource() = result and
606-
pragma[only_bind_out](result).getEnclosingCallable() = im and
607-
// This rules out updates in explicit initializer blocks as they are nested inside the compiler generated initializer blocks.
608-
pragma[only_bind_out](e.getEnclosingStmt().getParent()) = pragma[only_bind_out](im.getBody())
606+
exprStmt.getExpr() = e and
607+
// This check also rules out assignments in explicit initializer blocks
608+
// (CodeQL models explicit initializer blocks as BlockStmt in initializer methods)
609+
exprStmt.getParent() = im.getBody()
609610
)
610611
}
611612

0 commit comments

Comments
 (0)