| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,5 +14,7 @@ import semmle.code.csharp.frameworks.Format | |||
| 14 | 14 | import FormatFlow | |
| 15 | 15 | ||
| 16 | 16 | from FormatCall s, InvalidFormatString src, PathNode source, PathNode sink | |
| 17 | - where hasFlowPath(src, source, s, sink) | ||
| 17 | + where | ||
| 18 | + hasFlowPath(src, source, s, sink) and | ||
| 19 | + s.hasInsertions() | ||
| 18 | 20 | select src, source, sink, "Invalid format string used in $@ formatting call.", s, "this" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,6 @@ class Bad | |||
| 4 | 4 | { | |
| 5 | 5 | string GenerateEmptyClass(string c) | |
| 6 | 6 | { | |
| 7 | - return string.Format("class {0} { }"); | ||
| 7 | + return string.Format("class {0} { }", "C"); | ||
| 8 | 8 | } | |
| 9 | 9 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,7 @@ class FormatStringConfiguration extends TaintTracking::Configuration { | |||
| 27 | 27 | } | |
| 28 | 28 | ||
| 29 | 29 | override predicate isSink(DataFlow::Node sink) { | |
| 30 | - sink.asExpr() = any(FormatCall call).getFormatExpr() | ||
| 30 | + sink.asExpr() = any(FormatCall call | call.hasInsertions()).getFormatExpr() | ||
| 31 | 31 | } | |
| 32 | 32 | } | |
| 33 | 33 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -218,6 +218,9 @@ class FormatCall extends MethodCall { | |||
| 218 | 218 | /** Gets the argument number of the first supplied insert. */ | |
| 219 | 219 | int getFirstArgument() { result = this.getFormatArgument() + 1 } | |
| 220 | 220 | ||
| 221 | + /** Holds if this call has one or more insertions. */ | ||
| 222 | + predicate hasInsertions() { exists(this.getArgument(this.getFirstArgument())) } | ||
| 223 | + | ||
| 221 | 224 | /** Holds if the arguments are supplied in an array, not individually. */ | |
| 222 | 225 | predicate hasArrayExpr() { | |
| 223 | 226 | this.getNumberOfArguments() = this.getFirstArgument() + 1 and | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -115,6 +115,8 @@ void FormatMethodTests() | |||
| 115 | 115 | System.Diagnostics.Debug.Assert(true, "Error", "}", ps); | |
| 116 | 116 | sw.Write("}", 0); | |
| 117 | 117 | System.Diagnostics.Debug.Print("}", ps); | |
| 118 | + | ||
| 119 | + Console.WriteLine("}"); // GOOD | ||
| 118 | 120 | } | |
| 119 | 121 | ||
| 120 | 122 | System.IO.StringWriter sw; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,6 +51,7 @@ nodes | |||
| 51 | 51 | | FormatInvalid.cs:115:56:115:58 | [assertion success] "}" | semmle.label | [assertion success] "}" | | |
| 52 | 52 | | FormatInvalid.cs:116:18:116:20 | "}" | semmle.label | "}" | | |
| 53 | 53 | | FormatInvalid.cs:117:40:117:42 | "}" | semmle.label | "}" | | |
| 54 | + | FormatInvalid.cs:119:27:119:29 | "}" | semmle.label | "}" | | ||
| 54 | 55 | | FormatInvalidBad.cs:7:30:7:44 | "class {0} { }" | semmle.label | "class {0} { }" | | |
| 55 | 56 | | FormatInvalidGood.cs:7:30:7:46 | "class {0} {{ }}" | semmle.label | "class {0} {{ }}" | | |
| 56 | 57 | edges | |
@@ -96,4 +97,4 @@ edges | |||
| 96 | 97 | | FormatInvalid.cs:115:57:115:58 | "}" | FormatInvalid.cs:115:56:115:58 | [assertion success] "}" | FormatInvalid.cs:115:56:115:58 | [assertion success] "}" | Invalid format string used in $@ formatting call. | FormatInvalid.cs:115:9:115:63 | call to method Assert | this | | |
| 97 | 98 | | FormatInvalid.cs:116:19:116:20 | "}" | FormatInvalid.cs:116:18:116:20 | "}" | FormatInvalid.cs:116:18:116:20 | "}" | Invalid format string used in $@ formatting call. | FormatInvalid.cs:116:9:116:24 | call to method Write | this | | |
| 98 | 99 | | FormatInvalid.cs:117:41:117:42 | "}" | FormatInvalid.cs:117:40:117:42 | "}" | FormatInvalid.cs:117:40:117:42 | "}" | Invalid format string used in $@ formatting call. | FormatInvalid.cs:117:9:117:47 | call to method Print | this | | |
| 99 | - | FormatInvalidBad.cs:7:41:7:44 | "class {0} { }" | FormatInvalidBad.cs:7:30:7:44 | "class {0} { }" | FormatInvalidBad.cs:7:30:7:44 | "class {0} { }" | Invalid format string used in $@ formatting call. | FormatInvalidBad.cs:7:16:7:45 | call to method Format | this | | ||
| 100 | + | FormatInvalidBad.cs:7:41:7:44 | "class {0} { }" | FormatInvalidBad.cs:7:30:7:44 | "class {0} { }" | FormatInvalidBad.cs:7:30:7:44 | "class {0} { }" | Invalid format string used in $@ formatting call. | FormatInvalidBad.cs:7:16:7:50 | call to method Format | this | | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,6 @@ class Bad | |||
| 4 | 4 | { | |
| 5 | 5 | string GenerateEmptyClass(string c) | |
| 6 | 6 | { | |
| 7 | - return string.Format("class {0} { }"); | ||
| 7 | + return string.Format("class {0} { }", "C"); | ||
| 8 | 8 | } | |
| 9 | 9 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,9 @@ public void ProcessRequest(HttpContext ctx) | |||
| 21 | 21 | ||
| 22 | 22 | // GOOD: Not the format string. | |
| 23 | 23 | String.Format((IFormatProvider)null, "Do not do this", path); | |
| 24 | + | ||
| 25 | + // GOOD: Not a formatting call | ||
| 26 | + Console.WriteLine(path); | ||
| 24 | 27 | } | |
| 25 | 28 | ||
| 26 | 29 | System.Windows.Forms.TextBox box1; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,11 +6,11 @@ nodes | |||
| 6 | 6 | | UncontrolledFormatString.cs:11:23:11:45 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection | | |
| 7 | 7 | | UncontrolledFormatString.cs:14:23:14:26 | access to local variable path | semmle.label | access to local variable path | | |
| 8 | 8 | | UncontrolledFormatString.cs:17:46:17:49 | access to local variable path | semmle.label | access to local variable path | | |
| 9 | - | UncontrolledFormatString.cs:31:23:31:31 | access to property Text | semmle.label | access to property Text | | ||
| 9 | + | UncontrolledFormatString.cs:34:23:34:31 | access to property Text | semmle.label | access to property Text | | ||
| 10 | 10 | | UncontrolledFormatStringBad.cs:9:25:9:47 | access to property QueryString : NameValueCollection | semmle.label | access to property QueryString : NameValueCollection | | |
| 11 | 11 | | UncontrolledFormatStringBad.cs:12:39:12:44 | access to local variable format | semmle.label | access to local variable format | | |
| 12 | 12 | #select | |
| 13 | 13 | | UncontrolledFormatString.cs:14:23:14:26 | access to local variable path | UncontrolledFormatString.cs:11:23:11:45 | access to property QueryString : NameValueCollection | UncontrolledFormatString.cs:14:23:14:26 | access to local variable path | $@ flows to here and is used as a format string. | UncontrolledFormatString.cs:11:23:11:45 | access to property QueryString | access to property QueryString | | |
| 14 | 14 | | UncontrolledFormatString.cs:17:46:17:49 | access to local variable path | UncontrolledFormatString.cs:11:23:11:45 | access to property QueryString : NameValueCollection | UncontrolledFormatString.cs:17:46:17:49 | access to local variable path | $@ flows to here and is used as a format string. | UncontrolledFormatString.cs:11:23:11:45 | access to property QueryString | access to property QueryString | | |
| 15 | - | UncontrolledFormatString.cs:31:23:31:31 | access to property Text | UncontrolledFormatString.cs:31:23:31:31 | access to property Text | UncontrolledFormatString.cs:31:23:31:31 | access to property Text | $@ flows to here and is used as a format string. | UncontrolledFormatString.cs:31:23:31:31 | access to property Text | access to property Text | | ||
| 15 | + | UncontrolledFormatString.cs:34:23:34:31 | access to property Text | UncontrolledFormatString.cs:34:23:34:31 | access to property Text | UncontrolledFormatString.cs:34:23:34:31 | access to property Text | $@ flows to here and is used as a format string. | UncontrolledFormatString.cs:34:23:34:31 | access to property Text | access to property Text | | ||
| 16 | 16 | | UncontrolledFormatStringBad.cs:12:39:12:44 | access to local variable format | UncontrolledFormatStringBad.cs:9:25:9:47 | access to property QueryString : NameValueCollection | UncontrolledFormatStringBad.cs:12:39:12:44 | access to local variable format | $@ flows to here and is used as a format string. | UncontrolledFormatStringBad.cs:9:25:9:47 | access to property QueryString | access to property QueryString | | |
| Back | FazBrowse Home | New Git URL |
0 commit comments