mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
C#: Convert tests for cs/dereferenced-value-may-be-null to use inline expectations.
This commit is contained in:
@@ -59,13 +59,13 @@ public class C
|
||||
|
||||
public void AssertNotNullTest()
|
||||
{
|
||||
var o1 = Maybe() ? null : new object();
|
||||
var o1 = Maybe() ? null : new object(); // $ Source[cs/dereferenced-value-may-be-null]
|
||||
AssertNonNull(o1);
|
||||
o1.ToString(); // GOOD (false positive)
|
||||
o1.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
var o2 = Maybe() ? null : new object();
|
||||
var o2 = Maybe() ? null : new object(); // $ Source[cs/dereferenced-value-may-be-null]
|
||||
AssertNonNull(o1);
|
||||
o2.ToString(); // BAD (maybe)
|
||||
o2.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
var o3 = Maybe() ? null : new object();
|
||||
Assert.IsNotNull(o3);
|
||||
@@ -91,16 +91,16 @@ public class C
|
||||
|
||||
public void Lock()
|
||||
{
|
||||
var o = Maybe() ? null : new object();
|
||||
lock (o) // BAD (maybe)
|
||||
var o = Maybe() ? null : new object(); // $ Source[cs/dereferenced-value-may-be-null]
|
||||
lock (o) // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
o.ToString(); // GOOD
|
||||
}
|
||||
|
||||
public void Foreach(IEnumerable<int> list)
|
||||
{
|
||||
if (Maybe())
|
||||
list = null;
|
||||
foreach (var x in list) // BAD (maybe)
|
||||
list = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
foreach (var x in list) // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
x.ToString(); // GOOD
|
||||
list.ToString(); // GOOD
|
||||
@@ -174,8 +174,8 @@ public class C
|
||||
s = "";
|
||||
do
|
||||
{
|
||||
s.ToString(); // BAD (maybe)
|
||||
s = null;
|
||||
s.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
s = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
while (true);
|
||||
}
|
||||
@@ -200,8 +200,8 @@ public class C
|
||||
s = "";
|
||||
while (true)
|
||||
{
|
||||
s.ToString(); // BAD (maybe)
|
||||
s = null;
|
||||
s.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
s = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,8 +219,8 @@ public class C
|
||||
|
||||
s = "";
|
||||
if (s != null && s.Length % 2 == 0)
|
||||
s = null;
|
||||
s.ToString(); // BAD (maybe)
|
||||
s = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
s.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void For()
|
||||
@@ -237,9 +237,9 @@ public class C
|
||||
s.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
|
||||
}
|
||||
|
||||
for (s = ""; ; s = null)
|
||||
for (s = ""; ; s = null) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
s.ToString(); // BAD (maybe)
|
||||
s.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,22 +14,22 @@ public class D
|
||||
public void Caller()
|
||||
{
|
||||
Callee1(new object());
|
||||
Callee1(null);
|
||||
Callee1(null); // $ Source[cs/dereferenced-value-may-be-null]
|
||||
Callee2(new object());
|
||||
}
|
||||
|
||||
public void Callee1(object param)
|
||||
{
|
||||
param.ToString(); // BAD (maybe)
|
||||
param.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void Callee2(object param)
|
||||
public void Callee2(object param) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
if (param != null)
|
||||
{
|
||||
param.ToString(); // GOOD
|
||||
}
|
||||
param.ToString(); // BAD (maybe)
|
||||
param.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
private static bool CustomIsNull(object x)
|
||||
@@ -55,54 +55,54 @@ public class D
|
||||
if ((2 > 1 && o4 != null) != false)
|
||||
o4.ToString(); // GOOD
|
||||
|
||||
var o5 = (o4 != null) ? "" : null;
|
||||
var o5 = (o4 != null) ? "" : null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (o5 != null)
|
||||
o4.ToString(); // GOOD
|
||||
if (o4 != null)
|
||||
o5.ToString(); // GOOD (false positive)
|
||||
o5.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
var o6 = maybe ? null : "";
|
||||
if (!CustomIsNull(o6))
|
||||
o6.ToString(); // GOOD
|
||||
|
||||
var o7 = maybe ? null : "";
|
||||
var o7 = maybe ? null : ""; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
var ok = o7 != null && 2 > 1;
|
||||
if (ok)
|
||||
o7.ToString(); // GOOD
|
||||
else
|
||||
o7.ToString(); // BAD (maybe)
|
||||
o7.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
var o8 = maybe ? null : "";
|
||||
var o8 = maybe ? null : ""; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
int track = o8 == null ? 42 : 1 + 1;
|
||||
if (track == 2)
|
||||
o8.ToString(); // GOOD
|
||||
if (track != 42)
|
||||
o8.ToString(); // GOOD
|
||||
if (track < 42)
|
||||
o8.ToString(); // GOOD (false positive)
|
||||
o8.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
if (track <= 41)
|
||||
o8.ToString(); // GOOD (false positive)
|
||||
o8.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void Deref(int i)
|
||||
{
|
||||
int[] xs = maybe ? null : new int[2];
|
||||
int[] xs = maybe ? null : new int[2]; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (i > 1)
|
||||
xs[0] = 5; // BAD (maybe)
|
||||
xs[0] = 5; // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
if (i > 2)
|
||||
maybe = xs[1] > 5; // BAD (maybe)
|
||||
maybe = xs[1] > 5; // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
if (i > 3)
|
||||
{
|
||||
var l = xs.Length; // BAD (maybe)
|
||||
var l = xs.Length; // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
if (i > 4)
|
||||
foreach (var _ in xs) ; // BAD (maybe)
|
||||
foreach (var _ in xs) ; // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
if (i > 5)
|
||||
lock (xs) // BAD (maybe)
|
||||
lock (xs) // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
xs.ToString(); // Not reported - same basic block
|
||||
|
||||
if (i > 6)
|
||||
@@ -122,7 +122,7 @@ public class D
|
||||
x.ToString(); // GOOD
|
||||
}
|
||||
|
||||
public void LengthGuard(int[] a, int[] b)
|
||||
public void LengthGuard(int[] a, int[] b) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
int alen = a == null ? 0 : a.Length; // GOOD
|
||||
int blen = b == null ? 0 : b.Length; // GOOD
|
||||
@@ -131,8 +131,8 @@ public class D
|
||||
{
|
||||
for (int i = 0; i < alen; i++)
|
||||
{
|
||||
sum += a[i]; // GOOD (false positive)
|
||||
sum += b[i]; // GOOD (false positive)
|
||||
sum += a[i]; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
sum += b[i]; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
int alen2;
|
||||
@@ -142,13 +142,13 @@ public class D
|
||||
alen2 = 0;
|
||||
for (int i = 1; i <= alen2; ++i)
|
||||
{
|
||||
sum += a[i - 1]; // GOOD (false positive)
|
||||
sum += a[i - 1]; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
|
||||
public void MissedGuard(object obj)
|
||||
public void MissedGuard(object obj) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
obj.ToString(); // BAD (maybe)
|
||||
obj.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
var x = obj != null ? 1 : 0;
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ public class D
|
||||
|
||||
public void Exceptions()
|
||||
{
|
||||
object obj = null;
|
||||
object obj = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
try
|
||||
{
|
||||
obj = MkMaybe();
|
||||
@@ -168,7 +168,7 @@ public class D
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
obj.ToString(); // BAD (maybe)
|
||||
obj.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
object obj2 = null;
|
||||
try
|
||||
@@ -237,25 +237,25 @@ public class D
|
||||
if (flag)
|
||||
o.ToString(); // GOOD
|
||||
|
||||
o = null;
|
||||
o = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
var other = maybe ? null : "";
|
||||
if (other == null)
|
||||
o = "";
|
||||
if (other != null)
|
||||
o.ToString(); // BAD (always) (reported as maybe)
|
||||
o.ToString(); // $ Alert[cs/dereferenced-value-may-be-null] (always - but reported as maybe)
|
||||
else
|
||||
o.ToString(); // GOOD (false positive)
|
||||
o.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
var o2 = (num < 0) ? null : "";
|
||||
var o2 = (num < 0) ? null : ""; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (num < 0)
|
||||
o2 = "";
|
||||
else
|
||||
o2.ToString(); // GOOD (false positive)
|
||||
o2.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void TrackingVariable(int[] a)
|
||||
{
|
||||
object o = null;
|
||||
object o = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
object other = null;
|
||||
if (maybe)
|
||||
{
|
||||
@@ -264,9 +264,9 @@ public class D
|
||||
}
|
||||
|
||||
if (other is string)
|
||||
o.ToString(); // GOOD (false positive)
|
||||
o.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
o = null;
|
||||
o = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
int count = 0;
|
||||
var found = false;
|
||||
for (var i = 0; i < a.Length; i++)
|
||||
@@ -280,7 +280,7 @@ public class D
|
||||
}
|
||||
if (a[i] > 10000)
|
||||
{
|
||||
o = null;
|
||||
o = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
count = 0;
|
||||
if (2 > i) { }
|
||||
found = false;
|
||||
@@ -288,20 +288,20 @@ public class D
|
||||
}
|
||||
|
||||
if (count > 3)
|
||||
o.ToString(); // GOOD (false positive)
|
||||
o.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
if (found)
|
||||
o.ToString(); // GOOD (false positive)
|
||||
o.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
object prev = null;
|
||||
object prev = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
for (var i = 0; i < a.Length; ++i)
|
||||
{
|
||||
if (i != 0)
|
||||
prev.ToString(); // GOOD (false positive)
|
||||
prev.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
prev = a[i];
|
||||
}
|
||||
|
||||
string s = null;
|
||||
string s = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
var s_null = true;
|
||||
foreach (var i in a)
|
||||
@@ -310,10 +310,10 @@ public class D
|
||||
s = "" + a;
|
||||
}
|
||||
if (!s_null)
|
||||
s.ToString(); // GOOD (false positive)
|
||||
s.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
object r = null;
|
||||
object r = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
var stat = MyStatus.INIT;
|
||||
while (stat == MyStatus.INIT && stat != MyStatus.READY)
|
||||
{
|
||||
@@ -321,7 +321,7 @@ public class D
|
||||
if (stat == MyStatus.INIT)
|
||||
stat = MyStatus.READY;
|
||||
}
|
||||
r.ToString(); // GOOD (false positive)
|
||||
r.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public enum MyStatus
|
||||
@@ -348,28 +348,28 @@ public class D
|
||||
|
||||
public void LoopCorr(int iters)
|
||||
{
|
||||
int[] a = null;
|
||||
int[] a = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (iters > 0)
|
||||
a = new int[iters];
|
||||
|
||||
for (var i = 0; i < iters; ++i)
|
||||
a[i] = 0; // GOOD (false positive)
|
||||
a[i] = 0; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
if (iters > 0)
|
||||
{
|
||||
string last = null;
|
||||
string last = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
for (var i = 0; i < iters; i++)
|
||||
last = "abc";
|
||||
last.ToString(); // GOOD (false positive)
|
||||
last.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
int[] b = maybe ? null : new int[iters];
|
||||
int[] b = maybe ? null : new int[iters]; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (iters > 0 && (b == null || b.Length < iters))
|
||||
throw new Exception();
|
||||
|
||||
for (var i = 0; i < iters; ++i)
|
||||
{
|
||||
b[i] = 0; // GOOD (false positive)
|
||||
b[i] = 0; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,30 +385,30 @@ public class D
|
||||
ioe.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
|
||||
}
|
||||
|
||||
public void LengthGuard2(int[] a, int[] b)
|
||||
public void LengthGuard2(int[] a, int[] b) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
int alen = a == null ? 0 : a.Length; // GOOD
|
||||
int sum = 0;
|
||||
int i;
|
||||
for (i = 0; i < alen; i++)
|
||||
{
|
||||
sum += a[i]; // GOOD (false positive)
|
||||
sum += a[i]; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
int blen = b == null ? 0 : b.Length; // GOOD
|
||||
for (i = 0; i < blen; i++)
|
||||
{
|
||||
sum += b[i]; // GOOD (false positive)
|
||||
sum += b[i]; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
i = -3;
|
||||
}
|
||||
|
||||
public void CorrConds2(object x, object y)
|
||||
public void CorrConds2(object x, object y) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
if ((x != null && y == null) || (x == null && y != null))
|
||||
return;
|
||||
if (x != null)
|
||||
y.ToString(); // GOOD (false positive)
|
||||
y.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
if (y != null)
|
||||
x.ToString(); // GOOD (false positive)
|
||||
x.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ public class E
|
||||
{
|
||||
public void Ex1(long[][][] a1, int ix, int len)
|
||||
{
|
||||
long[][] a2 = null;
|
||||
long[][] a2 = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
var haveA2 = ix < len && (a2 = a1[ix]) != null;
|
||||
long[] a3 = null;
|
||||
var haveA3 = haveA2 && (a3 = a2[ix]) != null; // GOOD (FALSE POSITIVE)
|
||||
long[] a3 = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
var haveA3 = haveA2 && (a3 = a2[ix]) != null; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
if (haveA3)
|
||||
a3[0] = 0; // GOOD (FALSE POSITIVE)
|
||||
a3[0] = 0; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void Ex2(bool x, bool y)
|
||||
@@ -20,11 +20,11 @@ public class E
|
||||
var s2 = (s1 == null) ? null : "";
|
||||
if (s2 == null)
|
||||
{
|
||||
s1 = y ? null : "";
|
||||
s1 = y ? null : ""; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
s2 = (s1 == null) ? null : "";
|
||||
}
|
||||
if (s2 != null)
|
||||
s1.ToString(); // GOOD (FALSE POSITIVE)
|
||||
s1.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void Ex3(IEnumerable<string> ss)
|
||||
@@ -48,7 +48,7 @@ public class E
|
||||
{
|
||||
int index = 0;
|
||||
var result = new List<List<string>>();
|
||||
List<string> slice = null;
|
||||
List<string> slice = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
var iter = list.GetEnumerator();
|
||||
while (iter.MoveNext())
|
||||
{
|
||||
@@ -58,19 +58,19 @@ public class E
|
||||
slice = new List<string>();
|
||||
result.Add(slice);
|
||||
}
|
||||
slice.Add(str); // GOOD (FALSE POSITIVE)
|
||||
slice.Add(str); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
public void Ex5(bool hasArr, int[] arr)
|
||||
public void Ex5(bool hasArr, int[] arr) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
int arrLen = 0;
|
||||
if (hasArr)
|
||||
arrLen = arr == null ? 0 : arr.Length;
|
||||
|
||||
if (arrLen > 0)
|
||||
arr[0] = 0; // GOOD (FALSE POSITIVE)
|
||||
arr[0] = 0; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public const int MY_CONST_A = 1;
|
||||
@@ -104,12 +104,12 @@ public class E
|
||||
|
||||
public void Ex7(int[] arr1)
|
||||
{
|
||||
int[] arr2 = null;
|
||||
int[] arr2 = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (arr1.Length > 0)
|
||||
arr2 = new int[arr1.Length];
|
||||
|
||||
for (var i = 0; i < arr1.Length; i++)
|
||||
arr2[i] = arr1[i]; // GOOD (FALSE POSITIVE)
|
||||
arr2[i] = arr1[i]; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void Ex8(int x, int lim)
|
||||
@@ -122,7 +122,7 @@ public class E
|
||||
int j = 0;
|
||||
while (!stop && j < lim)
|
||||
{
|
||||
int step = (j * obj.GetHashCode()) % 10; // GOOD (FALSE POSITIVE)
|
||||
int step = (j * obj.GetHashCode()) % 10; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
if (step == 0)
|
||||
{
|
||||
obj.ToString(); // GOOD
|
||||
@@ -134,7 +134,7 @@ public class E
|
||||
}
|
||||
else
|
||||
{
|
||||
obj = null;
|
||||
obj = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -149,33 +149,33 @@ public class E
|
||||
{
|
||||
return;
|
||||
}
|
||||
object obj2 = obj1;
|
||||
object obj2 = obj1; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (obj2 != null && obj2.GetHashCode() % 5 > 2)
|
||||
{
|
||||
obj2.ToString(); // GOOD
|
||||
cond = true;
|
||||
}
|
||||
if (cond)
|
||||
obj2.ToString(); // GOOD (FALSE POSITIVE)
|
||||
obj2.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void Ex10(int[] a)
|
||||
public void Ex10(int[] a) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
int n = a == null ? 0 : a.Length;
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
int x = a[i]; // GOOD (FALSE POSITIVE)
|
||||
int x = a[i]; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
if (x > 7)
|
||||
a = new int[n];
|
||||
}
|
||||
}
|
||||
|
||||
public void Ex11(object obj, bool b1)
|
||||
public void Ex11(object obj, bool b1) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
bool b2 = obj == null ? false : b1;
|
||||
if (b2 == null)
|
||||
{
|
||||
obj.ToString(); // GOOD (FALSE POSITIVE)
|
||||
obj.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
@@ -183,24 +183,24 @@ public class E
|
||||
}
|
||||
if (b1 == null)
|
||||
{
|
||||
obj.ToString(); // GOOD (FALSE POSITIVE)
|
||||
obj.ToString(); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
|
||||
public void Ex12(object o)
|
||||
public void Ex12(object o) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
var i = o.GetHashCode(); // BAD (maybe)
|
||||
var i = o.GetHashCode(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
var s = o?.ToString();
|
||||
}
|
||||
|
||||
public void Ex13(bool b)
|
||||
{
|
||||
var o = b ? null : "";
|
||||
var o = b ? null : ""; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
o.M1(); // GOOD
|
||||
if (b)
|
||||
o.M2(); // BAD (maybe)
|
||||
o.M2(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
else
|
||||
o.Select(x => x); // BAD (maybe)
|
||||
o.Select(x => x); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public int Ex14(string s)
|
||||
@@ -214,8 +214,8 @@ public class E
|
||||
{
|
||||
var x = "";
|
||||
if (b)
|
||||
x = null;
|
||||
x.ToString(); // BAD (maybe)
|
||||
x = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
x.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
if (b)
|
||||
x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
|
||||
}
|
||||
@@ -224,20 +224,20 @@ public class E
|
||||
{
|
||||
var x = "";
|
||||
if (b)
|
||||
x = null;
|
||||
x = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
if (b)
|
||||
x.ToString(); // $ Alert[cs/dereferenced-value-is-always-null]
|
||||
x.ToString(); // BAD (maybe)
|
||||
x.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public int Ex17(int? i)
|
||||
public int Ex17(int? i) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
return i.Value; // BAD (maybe)
|
||||
return i.Value; // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public int Ex18(int? i)
|
||||
public int Ex18(int? i) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
return (int)i; // BAD (maybe)
|
||||
return (int)i; // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public int Ex19(int? i)
|
||||
@@ -280,9 +280,9 @@ public class E
|
||||
{
|
||||
if (b)
|
||||
b.ToString();
|
||||
var o = Make();
|
||||
var o = Make(); // $ Source[cs/dereferenced-value-may-be-null]
|
||||
o?.ToString();
|
||||
o.ToString(); // BAD (maybe)
|
||||
o.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
if (b)
|
||||
b.ToString();
|
||||
}
|
||||
@@ -298,8 +298,8 @@ public class E
|
||||
|
||||
public void Ex25(object o)
|
||||
{
|
||||
var s = o as string;
|
||||
s.ToString(); // BAD (maybe)
|
||||
var s = o as string; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
s.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
private long? l;
|
||||
@@ -339,14 +339,14 @@ public class E
|
||||
|
||||
static void Ex30(string s, object o)
|
||||
{
|
||||
var x = s ?? o as string;
|
||||
x.ToString(); // BAD (maybe)
|
||||
var x = s ?? o as string; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
x.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
static void Ex31(string s, object o)
|
||||
{
|
||||
dynamic x = s ?? o as string;
|
||||
x.ToString(); // BAD (maybe)
|
||||
dynamic x = s ?? o as string; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
x.ToString(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
static void Ex32(string s, object o)
|
||||
@@ -363,7 +363,7 @@ public class E
|
||||
x.ToString(); // GOOD
|
||||
}
|
||||
|
||||
static int Ex34(string s = null) => s.Length; // BAD (maybe)
|
||||
static int Ex34(string s = null) => s.Length; // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
|
||||
static int Ex35(string s = "null") => s.Length; // GOOD
|
||||
|
||||
@@ -371,19 +371,19 @@ public class E
|
||||
{
|
||||
if (o is string)
|
||||
{
|
||||
var s = o as string;
|
||||
return s.Length; // GOOD (FALSE POSITIVE)
|
||||
var s = o as string; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
return s.Length; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool Ex37(E e1, E e2)
|
||||
static bool Ex37(E e1, E e2) // $ Source[cs/dereferenced-value-may-be-null]
|
||||
{
|
||||
if ((e1 == null && e2 != null) || (e1 != null && e2 == null))
|
||||
return false;
|
||||
if (e1 == null && e2 == null)
|
||||
return true;
|
||||
return e1.Long == e2.Long; // GOOD (FALSE POSITIVE)
|
||||
return e1.Long == e2.Long; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
int Ex38(int? i)
|
||||
@@ -414,20 +414,20 @@ public class E
|
||||
|
||||
static bool Ex42(int? i, IEnumerable<int> @is)
|
||||
{
|
||||
return @is.Any(j => j == i.Value); // BAD (maybe)
|
||||
return @is.Any(j => j == i.Value); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
static bool Ex43(int? i, IEnumerable<int> @is)
|
||||
{
|
||||
if (i.HasValue)
|
||||
return @is.Any(j => j == i.Value); // GOOD (FALSE POSITIVE)
|
||||
return @is.Any(j => j == i.Value); // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool Ex44(int? i, IEnumerable<int> @is)
|
||||
{
|
||||
if (i.HasValue)
|
||||
@is = @is.Where(j => j == i.Value); // BAD (always)
|
||||
@is = @is.Where(j => j == i.Value); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
i = null;
|
||||
return @is.Any();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ class GuardedStringTest
|
||||
{
|
||||
void Fn(bool b)
|
||||
{
|
||||
string s = b ? null : "";
|
||||
string s = b ? null : ""; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
|
||||
if (!string.IsNullOrEmpty(s))
|
||||
{
|
||||
@@ -32,7 +32,7 @@ class GuardedStringTest
|
||||
Console.WriteLine(s.Length); // GOOD
|
||||
|
||||
if (s?.Length != 0)
|
||||
Console.WriteLine(s.Length); // BAD (maybe)
|
||||
Console.WriteLine(s.Length); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
else
|
||||
Console.WriteLine(s.Length); // GOOD
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
CSI/NullMaybe.ql
|
||||
query: CSI/NullMaybe.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -4,12 +4,12 @@ class Bad
|
||||
{
|
||||
void DoPrint(object o)
|
||||
{
|
||||
Console.WriteLine(o.ToString());
|
||||
Console.WriteLine(o.ToString()); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
void M()
|
||||
{
|
||||
DoPrint("Hello");
|
||||
DoPrint(null);
|
||||
DoPrint(null); // $ Source[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,12 @@ public class Params
|
||||
|
||||
public void M2(params string[] args)
|
||||
{
|
||||
var l = args.Length; // Good
|
||||
var l = args.Length; // $ SPURIOUS (false positive): Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
public void M()
|
||||
{
|
||||
M1("a", "b", "c", null);
|
||||
M2(null);
|
||||
M2(null); // $ Source[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ class StringsTest
|
||||
|
||||
void StringMaybeNull()
|
||||
{
|
||||
string s = null;
|
||||
string s = null; // $ Source[cs/dereferenced-value-may-be-null]
|
||||
while (s != "")
|
||||
s = s.Trim(); // BAD (maybe)
|
||||
s = s.Trim(); // $ Alert[cs/dereferenced-value-may-be-null]
|
||||
}
|
||||
|
||||
void StringNotNull()
|
||||
|
||||
Reference in New Issue
Block a user