forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Bugprone unsafe format string #3
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
Open
dkrupp
wants to merge
16
commits into
main
Choose a base branch
from
bugprone-unsafe-format-string
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bad2fdc
Initial commit of bugprone-unsafe-format-string check
dkrupp e752e54
remove MaxFieldWidth configuration option as it is not needed
dkrupp 688c582
Remove SuggestAlternatives configuration option as it is not needed
dkrupp 9947b3c
Changing the test from C++ to C
dkrupp a5f6717
Add wide character handling in the format string
dkrupp dd5eaca
Fixing the test cases to use string literals in the va_list tests
dkrupp 73a64ff
Handling wchar_t properly. Solving it manually (Q could not do it).
dkrupp 3902f9a
Adding checker documentation
dkrupp a3b3546
adding negative test cases
dkrupp e6199a5
handle sprintf and scanf differently
dkrupp fc91d74
Adding test cases with dynamic field width
dkrupp 69c6a13
Marking those tests negatve which expect a warning
dkrupp 7540250
Changing back positive/negative test nomenclature. Fixing up some tes…
dkrupp a6c6ad7
no need for the spec md file
dkrupp 77e2d97
Fixing review comments
dkrupp 795834a
Add system header simulator to the tests
dkrupp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 29 additions & 2 deletions
31
clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-format-string.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-format-string.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // RUN: %check_clang_tidy %s bugprone-unsafe-format-string %t | ||
|
|
||
| namespace std { | ||
| int sprintf( char* buffer, const char* format, ... ); | ||
| } | ||
|
|
||
| class TestClass{ | ||
| int sprintf( char* buffer, const char* format, ... ); | ||
| void test_sprintf() { | ||
| char buffer[100]; | ||
| const char* input = "user input"; | ||
| /* Negative: no warning for calling member functions */ | ||
| sprintf(buffer, "%s", input); | ||
| } | ||
| }; | ||
|
|
||
| void test_sprintf() { | ||
| char buffer[100]; | ||
| const char* input = "user input"; | ||
|
|
||
| /* Positive: unsafe %s without field width */ | ||
| std::sprintf(buffer, "%s", input); | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: format specifier '%s' without precision may cause buffer overflow; consider using '%.Ns' where N limits output length [bugprone-unsafe-format-string] | ||
|
|
||
| /* Positive: field width doesn't prevent overflow in sprintf */ | ||
| std::sprintf(buffer, "%99s", input); | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: format specifier '%s' without precision may cause buffer overflow; consider using '%.Ns' where N limits output length [bugprone-unsafe-format-string] | ||
|
|
||
| /* Positive: dynamic field width doesn't prevent overflow */ | ||
| std::sprintf(buffer, "%*s", 10, input); | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: format specifier '%s' without precision may cause buffer overflow; consider using '%.Ns' where N limits output length [bugprone-unsafe-format-string] | ||
|
|
||
| /*Negative: precision limits string length */ | ||
| std::sprintf(buffer, "%.99s", input); | ||
| /* no-warning */ | ||
|
|
||
| /*Negative: precision with field width */ | ||
| std::sprintf(buffer, "%1.99s", input); | ||
| /* no-warning */ | ||
|
|
||
| /*Negative: dynamic precision */ | ||
| std::sprintf(buffer, "%.*s", 99, input); | ||
| /* no-warning */ | ||
|
|
||
| /*Negative: field width with dynamic precision */ | ||
| std::sprintf(buffer, "%1.*s", 99, input); | ||
| /* no-warning */ | ||
|
|
||
| /*Negative: dynamic field width with fixed precision */ | ||
| std::sprintf(buffer, "%*.99s", 10, input); | ||
| /* no-warning */ | ||
|
|
||
| /*Negative: dynamic field width and precision */ | ||
| std::sprintf(buffer, "%*.*s", 10, 99, input); | ||
| /* no-warning */ | ||
|
|
||
| /*Negative: other format specifiers are safe */ | ||
| std::sprintf(buffer, "%d %f", 42, 3.14); | ||
| /* no-warning */ | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use a system header simulator file.