-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C#: Improve cs/invalid-string-formatting
and add to the Code Quality suite.
#19148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C#: Improve cs/invalid-string-formatting
and add to the Code Quality suite.
#19148
Conversation
cfe6cbd
to
286ae79
Compare
852eab9
to
e8f8145
Compare
DCA
|
51a03d2
to
be8d379
Compare
cs/invalid-string-formatting
and add to the codeql quality suite.
be8d379
to
5c6b1dd
Compare
cs/invalid-string-formatting
and add to the codeql quality suite.cs/invalid-string-formatting
and add to the Code Quality suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, thanks for breaking up into individual commits, that made reviewing a lot easier.
|
||
// GOOD: An array has been supplied. | ||
String.Format("{0} {1} {2}", args); | ||
|
||
// GOOD: All arguments supplied to params | ||
String.Format("{0} {1} {2} {3}", 0, 1, 2, 3); | ||
|
||
helper("{1}"); | ||
helper("{1}"); // $ Source |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be e.g. Source=source1
and then $ Alert=source1
further down.
|
||
// BAD: Invalid format string | ||
String.Format("%d", 1); | ||
String.Format("%d", 1); // $ Alert Sink |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the Sink
part is needed when it is on the same line as the alert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Sink and Alert doesn't have the exact same location (only the same line numbers), so the test fails, if we don't add both tags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CoPilot managed to do it 😸
class FormatMethod extends Method { | ||
FormatMethod() { | ||
exists(Class declType | declType = this.getDeclaringType() | | ||
abstract class FormatMethod extends Method { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we are exposing an abstract method, which is not backwards-compatible (and also not best practice). So Instead replace the current abstract FormatMethod
class with a private FormatMethodImpl
class, and add final class FormatMethod = FormatMethodImpl;
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, I see that you did exactly this in a subsequent commit.
void TestCompositeFormatMissingArgument() | ||
{ | ||
var format0 = CompositeFormat.Parse("{0}"); | ||
var format1 = CompositeFormat.Parse("{1}"); // $ Source |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, it is better to uniquely identify the sources so we can match them up against the alerts.
I think it is fine to keep as-is. |
5c6b1dd
to
b9183ff
Compare
c10b5ce
to
b9183ff
Compare
…tions and adjust some of the testcases.
…valid-string-format.
b9183ff
to
65ac951
Compare
In this PR we
cs/invalid-string-formatting
to the code quality suite.Improve the
cs/invalid-string-formatting
query bystring.Format
where no additional arguments are provided (for instancestring.Format("{")
). Also start takingCompositeFormat
versions ofFormat
methods into account and considerSystem.Text.CompositeFormat.Parse
a method that parses format strings and might throw exceptions and cause a runtime crash.string.Format
andStringBuilder.AppendFormat
and also add support forMemoryExtensions.TryWrite
.Console.WriteLine(string)
and friends (methods that itsn't format methods).Review commit by commit is encouraged.
@hvitved : The data flow configurations are quite similar. Is it a better approach to use flow state to track calls to
CompositeFormat.Parse
? (instead of merging two similar graphs).