Skip to content

Commit 11dffc6

Browse files
committed
C#: Add more invalid-string-formatting testcases.
1 parent 8fb5fe9 commit 11dffc6

File tree

4 files changed

+283
-118
lines changed

4 files changed

+283
-118
lines changed

csharp/ql/test/query-tests/API Abuse/FormatInvalid/FormatInvalid.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Text;
34

45
class FormatInvalid
@@ -134,5 +135,27 @@ void FormatMethodTests()
134135
ts.TraceInformation("{0}"); // GOOD
135136
}
136137

138+
void CompositeFormatMethods()
139+
{
140+
var format = CompositeFormat.Parse("}"); // $ Alert
141+
142+
// GOOD: Format is invalid and this flagged during parsing.
143+
String.Format<string>(null, format, "");
144+
String.Format<string, string>(null, format, "", "");
145+
String.Format<string, string, string>(null, format, "", "", "");
146+
147+
sb.AppendFormat(null, format, "");
148+
sb.AppendFormat<string>(null, format, "");
149+
sb.AppendFormat<string, string>(null, format, "", "");
150+
sb.AppendFormat<string, string, string>(null, format, "", "", "");
151+
152+
153+
var span = new Span<char>();
154+
span.TryWrite(null, format, out _);
155+
span.TryWrite<object>(null, format, out _, new object());
156+
span.TryWrite<object, object>(null, format, out _, new object(), new object());
157+
span.TryWrite<object, object, object>(null, format, out _, "", "", "");
158+
}
159+
137160
System.IO.StringWriter sw;
138161
}

0 commit comments

Comments
 (0)