Skip to content

Commit 8c67dee

Browse files
committed
Add and use exprRefersToNil predicate
1 parent 955c20d commit 8c67dee

File tree

6 files changed

+12
-10
lines changed

6 files changed

+12
-10
lines changed

go/ql/lib/semmle/go/Expr.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,8 @@ class ConstantName extends ValueName {
20432043
override string getAPrimaryQlClass() { result = "ConstantName" }
20442044
}
20452045

2046+
predicate exprRefersToNil(Expr e) { e.(ConstantName).getTarget() = Builtin::nil() }
2047+
20462048
/**
20472049
* A name referring to a variable.
20482050
*
@@ -2183,7 +2185,7 @@ private predicate isTypeExprTopDown(Expr e) {
21832185
or
21842186
e = any(TypeSwitchStmt s).getACase().getExpr(_) and
21852187
// special case: `nil` is allowed in a type case but isn't a type
2186-
not e = Builtin::nil().getAReference()
2188+
not exprRefersToNil(e)
21872189
or
21882190
e = any(SelectorExpr sel | isTypeExprTopDown(sel)).getBase()
21892191
or

go/ql/lib/semmle/go/dataflow/Properties.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Property extends TProperty {
2222
isTrue = eq.getPolarity().booleanXor(e.getBoolValue().booleanXor(outcome))
2323
or
2424
this = IsNil(isTrue) and
25-
e = Builtin::nil().getAReference() and
25+
exprRefersToNil(e) and
2626
isTrue = eq.getPolarity().booleanXor(outcome).booleanNot()
2727
)
2828
or

go/ql/lib/semmle/go/dataflow/internal/DataFlowUtil.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ private predicate onlyPossibleReturnOfBool(FuncDecl fd, FunctionOutput res, Node
560560
*/
561561
predicate possiblyReturnsNonNil(FuncDecl fd, FunctionOutput res, Node ret) {
562562
ret = res.getEntryNode(fd) and
563-
not ret.asExpr() = Builtin::nil().getAReference()
563+
not exprRefersToNil(ret.asExpr())
564564
}
565565

566566
/**
@@ -570,7 +570,7 @@ predicate possiblyReturnsNonNil(FuncDecl fd, FunctionOutput res, Node ret) {
570570
private predicate onlyPossibleReturnOfNonNil(FuncDecl fd, FunctionOutput res, Node ret) {
571571
possiblyReturnsNonNil(fd, res, ret) and
572572
forall(Node otherRet | otherRet = res.getEntryNode(fd) and otherRet != ret |
573-
otherRet.asExpr() = Builtin::nil().getAReference()
573+
exprRefersToNil(otherRet.asExpr())
574574
)
575575
}
576576

@@ -609,7 +609,7 @@ private predicate isCertainlyNotNil(DataFlow::Node node) {
609609
*/
610610
private predicate onlyPossibleReturnOfNil(FuncDecl fd, FunctionOutput res, DataFlow::Node ret) {
611611
ret = res.getEntryNode(fd) and
612-
ret.asExpr() = Builtin::nil().getAReference() and
612+
exprRefersToNil(ret.asExpr()) and
613613
forall(DataFlow::Node otherRet | otherRet = res.getEntryNode(fd) and otherRet != ret |
614614
isCertainlyNotNil(otherRet)
615615
)

go/ql/lib/semmle/go/dataflow/internal/TaintTrackingUtil.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ predicate functionEnsuresInputIsConstant(
418418
forex(DataFlow::Node ret, IR::ReturnInstruction ri |
419419
ret = outp.getEntryNode(fd) and
420420
ri.getReturnStmt().getAnExpr() = ret.asExpr() and
421-
ret.asExpr() = Builtin::nil().getAReference()
421+
exprRefersToNil(ret.asExpr())
422422
|
423423
DataFlow::localFlow(inp.getExitNode(fd), _) and
424424
mustPassConstantCaseTestToReach(ri, inp.getExitNode(fd))

go/ql/src/RedundantCode/UnreachableStatement.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ControlFlow::Node nonGuardPredecessor(ControlFlow::Node nd) {
2626
* Matches if `retval` is a constant or a struct composed wholly of constants.
2727
*/
2828
predicate isAllowedReturnValue(Expr retval) {
29-
retval = Builtin::nil().getAReference()
29+
exprRefersToNil(retval)
3030
or
3131
retval = Builtin::true_().getAReference()
3232
or

go/ql/src/experimental/CWE-203/Timing.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private class SensitiveStringCompareSink extends Sink {
3636
not op1 = nonSensitiveOperand and
3737
not (
3838
// Comparisons with `nil` should be excluded.
39-
nonSensitiveOperand = Builtin::nil().getAReference()
39+
exprRefersToNil(nonSensitiveOperand)
4040
or
4141
// Comparisons with empty string should also be excluded.
4242
nonSensitiveOperand.getStringValue().length() = 0
@@ -60,7 +60,7 @@ private class SensitiveCompareSink extends Sink {
6060
not op1 = op2 and
6161
not (
6262
// Comparisons with `nil` should be excluded.
63-
op2 = Builtin::nil().getAReference()
63+
exprRefersToNil(op2)
6464
or
6565
// Comparisons with empty string should also be excluded.
6666
op2.getStringValue().length() = 0
@@ -85,7 +85,7 @@ private class SensitiveStringSink extends Sink {
8585
not op1 = op2 and
8686
not (
8787
// Comparisons with `nil` should be excluded.
88-
op2 = Builtin::nil().getAReference()
88+
exprRefersToNil(op2)
8989
or
9090
// Comparisons with empty string should also be excluded.
9191
op2.getStringValue().length() = 0

0 commit comments

Comments
 (0)