Skip to content

Commit e1e657c

Browse files
committed
C#: Address review comments and update tests.
1 parent dd64cd2 commit e1e657c

File tree

7 files changed

+36
-15
lines changed

7 files changed

+36
-15
lines changed

csharp/ql/src/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ module Internal {
657657
not mc.isConditional()
658658
)
659659
or
660-
e.(DefaultValueExpr).getType() instanceof ValueType
660+
e.(DefaultValueExpr).getType().isValueType()
661661
}
662662

663663
/** Holds if expression `e2` is a non-`null` value whenever `e1` is. */

csharp/ql/src/semmle/code/csharp/dataflow/Nullness.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class AlwaysNullExpr extends Expr {
5959
or
6060
this.(Cast).getExpr() instanceof AlwaysNullExpr
6161
or
62-
this instanceof DefaultValueExpr and this.getType() instanceof RefType
62+
this instanceof DefaultValueExpr and this.getType().isRefType()
6363
or
6464
exists(Callable target |
6565
this.(Call).getTarget() = target and
@@ -83,7 +83,8 @@ class NonNullExpr extends Expr {
8383
exists(Callable target |
8484
this.(Call).getTarget() = target and
8585
not target.(Virtualizable).isVirtual() and
86-
alwaysNotNullCallable(target)
86+
alwaysNotNullCallable(target) and
87+
not this.(QualifiableExpr).isConditional()
8788
)
8889
}
8990
}

csharp/ql/src/semmle/code/csharp/dataflow/internal/CallableReturns.qll

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Provides predicates for analysing the return values of callables.
2+
* Provides predicates for analyzing the return values of callables.
33
*/
44

55
import csharp
@@ -11,14 +11,14 @@ private import semmle.code.cil.CallableReturns as CR
1111
predicate alwaysNullCallable(Callable c) {
1212
exists(CIL::Method m | m.matchesHandle(c) | CR::alwaysNullMethod(m))
1313
or
14-
forex(Expr e | callableReturns(c, e) | e instanceof AlwaysNullExpr)
14+
forex(Expr e | c.canReturn(e) | e instanceof AlwaysNullExpr)
1515
}
1616

1717
/** Holds if callable `c` always returns a non-null value. */
1818
predicate alwaysNotNullCallable(Callable c) {
1919
exists(CIL::Method m | m.matchesHandle(c) | CR::alwaysNotNullMethod(m))
2020
or
21-
forex(Expr e | callableReturns(c, e) | e instanceof NonNullExpr)
21+
forex(Expr e | c.canReturn(e) | e instanceof NonNullExpr)
2222
}
2323

2424
/** Holds if callable 'c' always throws an exception. */
@@ -39,12 +39,3 @@ predicate alwaysThrowsException(Callable c, Class ex) {
3939
exists(CIL::Method m, CIL::Type t | m.matchesHandle(c) | CR::alwaysThrowsException(m, t) and t.matchesHandle(ex))
4040
}
4141

42-
/** Holds if callable `m' can return expression `ret` directly or indirectly. */
43-
private predicate callableReturns(Callable m, Expr e) {
44-
exists(DataFlow::Node srcNode, DataFlow::Node retNode |
45-
e = srcNode.asExpr() and m.canReturn(retNode.asExpr())
46-
|
47-
DataFlow::localFlow(srcNode, retNode)
48-
)
49-
}
50-

csharp/ql/test/library-tests/cil/dataflow/DataFlow.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
| dataflow.cs:20:45:20:53 | "tainted" | dataflow.cs:20:18:20:54 | call to method GetFullPath |
66
| dataflow.cs:27:44:27:46 | 1 | dataflow.cs:27:18:27:52 | call to method IEEERemainder |
77
| dataflow.cs:27:49:27:51 | 2 | dataflow.cs:27:18:27:52 | call to method IEEERemainder |
8+
| dataflow.cs:64:30:64:33 | null | dataflow.cs:61:23:61:50 | ... ? ... : ... |
9+
| dataflow.cs:64:30:64:33 | null | dataflow.cs:70:20:70:33 | call to method IndirectNull |
10+
| dataflow.cs:71:23:71:26 | null | dataflow.cs:61:23:61:50 | ... ? ... : ... |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
alwaysNull
22
| dataflow.cs:54:21:54:35 | default(...) |
33
| dataflow.cs:56:27:56:56 | access to property DeclaringMethod |
4+
| dataflow.cs:58:39:58:52 | call to method IndirectNull |
45
alwaysNotNull
56
| dataflow.cs:55:23:55:34 | default(...) |
67
| dataflow.cs:56:27:56:30 | this access |
78
| dataflow.cs:56:27:56:40 | call to method GetType |
89
| dataflow.cs:57:30:57:33 | true |
910
| dataflow.cs:57:30:57:44 | call to method ToString |
11+
| dataflow.cs:58:21:58:34 | this access |
12+
| dataflow.cs:58:39:58:52 | this access |
13+
| dataflow.cs:61:23:61:26 | this access |
14+
| dataflow.cs:61:30:61:43 | this access |
15+
| dataflow.cs:61:47:61:50 | this access |

csharp/ql/test/library-tests/cil/dataflow/TaintTracking.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@
1111
| dataflow.cs:27:49:27:51 | 2 | dataflow.cs:27:18:27:52 | call to method IEEERemainder |
1212
| dataflow.cs:30:60:30:60 | 1 | dataflow.cs:30:18:30:80 | call to method DivRem |
1313
| dataflow.cs:30:63:30:63 | 2 | dataflow.cs:30:18:30:80 | call to method DivRem |
14+
| dataflow.cs:64:30:64:33 | null | dataflow.cs:58:21:58:52 | ... ?? ... |
15+
| dataflow.cs:64:30:64:33 | null | dataflow.cs:61:23:61:50 | ... ? ... : ... |
16+
| dataflow.cs:64:30:64:33 | null | dataflow.cs:70:20:70:33 | call to method IndirectNull |
17+
| dataflow.cs:71:23:71:26 | null | dataflow.cs:58:21:58:52 | ... ?? ... |
18+
| dataflow.cs:71:23:71:26 | null | dataflow.cs:61:23:61:50 | ... ? ... : ... |

csharp/ql/test/library-tests/cil/dataflow/dataflow.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,20 @@ void Nullness()
5555
var nonNull = default(int);
5656
var nullFromCil = this.GetType().DeclaringMethod;
5757
var nonNullFromCil = true.ToString();
58+
var null2 = NullFunction() ?? IndirectNull();
59+
60+
// The following are not always null:
61+
var notNull = cond ? NullFunction() : this;
62+
}
63+
64+
object IndirectNull() => null;
65+
66+
bool cond;
67+
68+
object NullFunction()
69+
{
70+
object x = IndirectNull();
71+
if (cond) x = null;
72+
return x;
5873
}
5974
}

0 commit comments

Comments
 (0)