Skip to content

Commit 2a9166f

Browse files
committed
C#: Add MaD tests for extensions.
1 parent da46ffa commit 2a9166f

File tree

4 files changed

+264
-1
lines changed

4 files changed

+264
-1
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/internal/ExternalFlow.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ module ModelValidation {
214214
not namespace.regexpMatch("[a-zA-Z0-9_\\.]+") and
215215
result = "Dubious namespace \"" + namespace + "\" in " + pred + " model."
216216
or
217-
not type.regexpMatch("[a-zA-Z0-9_<>,\\+]+") and
217+
not type.regexpMatch("[a-zA-Z0-9_<>,\\(\\)\\+]+") and
218218
result = "Dubious type \"" + type + "\" in " + pred + " model."
219219
or
220220
not name.regexpMatch("[a-zA-Z0-9_<>,\\.]*") and

csharp/ql/test/library-tests/dataflow/external-models/ExternalFlow.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,98 @@ public void M1()
348348
static void Sink(object o) { }
349349
}
350350

351+
// Test extensions
352+
public static class TestExtensions
353+
{
354+
extension(object o)
355+
{
356+
public object Method1() => throw null;
357+
public static object StaticMethod1(object s) => throw null;
358+
public object Property1 { get { throw null; } set { throw null; } }
359+
}
360+
361+
extension<T>(T t) where T : class
362+
{
363+
public T GenericMethod1() => throw null;
364+
public static T GenericStaticMethod1(T t0) => throw null;
365+
public T GenericProperty1 { get { throw null; } set { throw null; } }
366+
}
367+
}
368+
369+
public class M
370+
{
371+
public void M1()
372+
{
373+
var obj = new object();
374+
var o1 = obj.Method1();
375+
Sink(o1);
376+
377+
var o2 = TestExtensions.Method1(obj);
378+
Sink(o2);
379+
}
380+
381+
public void M2()
382+
{
383+
var obj = new object();
384+
var o1 = object.StaticMethod1(obj);
385+
Sink(o1);
386+
387+
var o2 = TestExtensions.StaticMethod1(obj);
388+
Sink(o2);
389+
}
390+
391+
public void M3(object o)
392+
{
393+
var obj = new object();
394+
o.Property1 = obj;
395+
var o1 = o.Property1;
396+
Sink(o1);
397+
}
398+
399+
public void M4(object o)
400+
{
401+
var obj = new object();
402+
TestExtensions.set_Property1(o, obj);
403+
var o1 = TestExtensions.get_Property1(o);
404+
Sink(o1);
405+
}
406+
407+
public void M5()
408+
{
409+
var obj = new object();
410+
var o1 = obj.GenericMethod1();
411+
Sink(o1);
412+
413+
var o2 = TestExtensions.GenericMethod1(obj);
414+
Sink(o2);
415+
}
416+
417+
public void M6()
418+
{
419+
var obj = new object();
420+
var o1 = object.GenericStaticMethod1(obj);
421+
Sink(o1);
422+
423+
var o2 = TestExtensions.GenericStaticMethod1(obj);
424+
Sink(o2);
425+
}
426+
427+
public void M7(object o)
428+
{
429+
var obj = new object();
430+
o.GenericProperty1 = obj;
431+
var o1 = o.GenericProperty1;
432+
Sink(o1);
433+
}
434+
435+
public void M8(object o)
436+
{
437+
var obj = new object();
438+
TestExtensions.set_GenericProperty1(o, obj);
439+
var o1 = TestExtensions.get_GenericProperty1(o);
440+
Sink(o1);
441+
}
442+
443+
static void Sink(object o) { }
444+
}
351445
}

0 commit comments

Comments
 (0)