@@ -8,67 +8,91 @@ private import semmle.code.csharp.frameworks.System
88private import semmle.code.csharp.frameworks.system.Text
99
1010/** A method that formats a string, for example `string.Format()`. */
11- class FormatMethod extends Method {
12- FormatMethod ( ) {
13- exists ( Class declType | declType = this .getDeclaringType ( ) |
11+ abstract class FormatMethod extends Method {
12+ /**
13+ * Gets the argument containing the format string. For example, the argument of
14+ * `string.Format(IFormatProvider, String, Object)` is `1`.
15+ */
16+ abstract int getFormatArgument ( ) ;
17+ }
18+
19+ private class StringAndStringBuilderFormatMethods extends FormatMethod {
20+ StringAndStringBuilderFormatMethods ( ) {
21+ (
1422 this .getParameter ( 0 ) .getType ( ) instanceof SystemIFormatProviderInterface and
15- this .getParameter ( 1 ) .getType ( ) instanceof StringType and
23+ this .getParameter ( 1 ) .getType ( ) instanceof StringType
24+ or
25+ this .getParameter ( 0 ) .getType ( ) instanceof StringType
26+ ) and
27+ (
28+ this = any ( SystemStringClass c ) .getFormatMethod ( )
29+ or
30+ this = any ( SystemTextStringBuilderClass c ) .getAppendFormatMethod ( )
31+ )
32+ }
33+
34+ override int getFormatArgument ( ) {
35+ if this .getParameter ( 0 ) .getType ( ) instanceof SystemIFormatProviderInterface
36+ then result = 1
37+ else result = 0
38+ }
39+ }
40+
41+ private class SystemConsoleAndSystemIoTextWriterFormatMethods extends FormatMethod {
42+ SystemConsoleAndSystemIoTextWriterFormatMethods ( ) {
43+ this .getParameter ( 0 ) .getType ( ) instanceof StringType and
44+ exists ( Class declType | declType = this .getDeclaringType ( ) |
45+ this .hasName ( [ "Write" , "WriteLine" ] ) and
1646 (
17- this = any ( SystemStringClass c ) . getFormatMethod ( )
47+ declType . hasFullyQualifiedName ( "System" , "Console" )
1848 or
19- this = any ( SystemTextStringBuilderClass c ) . getAppendFormatMethod ( )
49+ declType . hasFullyQualifiedName ( "System.IO" , "TextWriter" )
2050 )
21- or
22- this .getParameter ( 0 ) .getType ( ) instanceof StringType and
51+ )
52+ }
53+
54+ override int getFormatArgument ( ) { result = 0 }
55+ }
56+
57+ private class SystemDiagnosticsDebugAssert extends FormatMethod {
58+ SystemDiagnosticsDebugAssert ( ) {
59+ this .hasName ( "Assert" ) and
60+ this .getDeclaringType ( ) .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" ) and
61+ this .getNumberOfParameters ( ) = 4
62+ }
63+
64+ override int getFormatArgument ( ) { result = 2 }
65+ }
66+
67+ private class SystemDiagnosticsFormatMethods extends FormatMethod {
68+ SystemDiagnosticsFormatMethods ( ) {
69+ this .getParameter ( 0 ) .getType ( ) instanceof StringType and
70+ exists ( Class declType |
71+ declType = this .getDeclaringType ( ) and
72+ declType .getNamespace ( ) .getFullName ( ) = "System.Diagnostics"
73+ |
74+ declType .hasName ( "Trace" ) and
2375 (
24- this = any ( SystemStringClass c ) .getFormatMethod ( )
25- or
26- this = any ( SystemTextStringBuilderClass c ) .getAppendFormatMethod ( )
27- or
28- ( this .hasName ( "Write" ) or this .hasName ( "WriteLine" ) ) and
29- (
30- declType .hasFullyQualifiedName ( "System" , "Console" )
31- or
32- declType .hasFullyQualifiedName ( "System.IO" , "TextWriter" )
33- or
34- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" ) and
35- this .getParameter ( 1 ) .getType ( ) instanceof ArrayType
36- )
76+ this .hasName ( "TraceError" )
3777 or
38- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Trace" ) and
39- (
40- this .hasName ( "TraceError" ) or
41- this .hasName ( "TraceInformation" ) or
42- this .hasName ( "TraceWarning" )
43- )
78+ this .hasName ( "TraceInformation" )
4479 or
45- this .hasName ( "TraceInformation" ) and
46- declType .hasFullyQualifiedName ( "System.Diagnostics" , "TraceSource" )
47- or
48- this .hasName ( "Print" ) and
49- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" )
80+ this .hasName ( "TraceWarning" )
5081 )
5182 or
52- this .hasName ( "Assert" ) and
53- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" ) and
54- this .getNumberOfParameters ( ) = 4
83+ declType .hasName ( "TraceSource" ) and this .hasName ( "TraceInformation" )
84+ or
85+ declType .hasName ( "Debug" ) and
86+ (
87+ this .hasName ( "Print" )
88+ or
89+ this .hasName ( [ "Write" , "WriteLine" ] ) and
90+ this .getParameter ( 1 ) .getType ( ) instanceof ArrayType
91+ )
5592 )
5693 }
5794
58- /**
59- * Gets the argument containing the format string. For example, the argument of
60- * `string.Format(IFormatProvider, String, Object)` is `1`.
61- */
62- int getFormatArgument ( ) {
63- if this .getParameter ( 0 ) .getType ( ) instanceof SystemIFormatProviderInterface
64- then result = 1
65- else
66- if
67- this .hasName ( "Assert" ) and
68- this .getDeclaringType ( ) .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" )
69- then result = 2
70- else result = 0
71- }
95+ override int getFormatArgument ( ) { result = 0 }
7296}
7397
7498pragma [ nomagic]
0 commit comments