-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang][analyzer] Improve checker 'unix.cstring.NotNullTerminated' #149106
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
balazske
wants to merge
2
commits into
llvm:main
Choose a base branch
from
balazske:cstring_notnullterm
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,158 @@ | ||
| // RUN: %clang_analyze_cc1 -analyzer-checker=unix.cstring.NotNullTerminated -verify %s | ||
|
|
||
| #include "Inputs/system-header-simulator.h" | ||
|
|
||
| extern void *malloc(size_t); | ||
|
|
||
| size_t test_addr_fn() { | ||
| return strlen((char *)&malloc); // expected-warning{{Argument to string length function is the address of the function 'malloc', which is not a null-terminated string}} | ||
| } | ||
|
|
||
| size_t test_addr_label() { | ||
| lab: | ||
| return strlen((char *)&&lab); // expected-warning{{Argument to string length function is the address of the label 'lab', which is not a null-terminated string}} | ||
| } | ||
|
|
||
| size_t test_init_compound(int i) { | ||
| char src1[6] = {1,2,3,4,5,6}; | ||
| char src2[6] = {1,2,3,0,5,6}; | ||
| switch (i) { | ||
| case 1: | ||
| return strlen(src1); // expected-warning{{Terminating zero missing from string passed as 1st argument to string length function}} | ||
| case 2: | ||
| return strlen(src1 + 1); // expected-warning{{Terminating zero missing from string}} | ||
| case 3: | ||
| return strlen(src2); | ||
| case 4: | ||
| return strlen(src2 + 4); // expected-warning{{Terminating zero missing from string}} | ||
| case 5: | ||
| return strlen(src2 + 3); | ||
| } | ||
| src1[5] = 0; | ||
| return strlen(src1); | ||
| } | ||
|
|
||
| size_t test_init_literal(int i) { | ||
| char src1[] = "abcdef"; | ||
| int l = strlen(src1); | ||
| src1[6] = '.'; | ||
| src1[3] = 0; | ||
| switch (i) { | ||
| case 1: | ||
| return strlen(src1); | ||
| case 2: | ||
| return strlen(src1 + 4); // expected-warning{{Terminating zero missing from string}} | ||
| } | ||
| return l; | ||
| } | ||
|
|
||
| size_t test_init_assign(int i, char a) { | ||
| char src[6]; | ||
| src[1] = '1'; | ||
| src[2] = '2'; | ||
| src[4] = '4'; | ||
| src[5] = '5'; | ||
|
|
||
| switch (i) { | ||
| case 0: | ||
| return strlen(src); | ||
| case 1: | ||
| return strlen(src + 1); | ||
| case 2: | ||
| return strlen(src + 2); | ||
| case 3: | ||
| return strlen(src + 3); | ||
| case 4: | ||
| return strlen(src + 4); // expected-warning{{Terminating zero missing from string}} | ||
| } | ||
| src[5] = a; | ||
| size_t l = strlen(src + 4); | ||
| src[5] = 0; | ||
| l += strlen(src + 4); | ||
| src[5] = '5'; | ||
| return l + strlen(src + 4); // expected-warning{{Terminating zero missing from string}} | ||
| } | ||
|
|
||
| size_t test_assign1() { | ||
| char str1[5] = {'0','1','2','3','4'}; | ||
| char str2[5]; | ||
| str2[0] = str1[0]; | ||
| str2[1] = str1[1]; | ||
| str2[4] = str1[4]; | ||
| size_t l = strlen(str2); | ||
| return l + strlen(str2 + 4); // expected-warning{{Terminating zero missing from string}} | ||
| } | ||
|
|
||
| size_t test_assign2() { | ||
| char str1[5] = {1,2,3,4,5}; | ||
| char str2[5]; | ||
| str2[0] = str1[0]; | ||
| str2[4] = str2[0]; | ||
| return strlen(str2 + 4); // expected-warning{{Terminating zero missing from string}} | ||
| } | ||
|
|
||
| void test_ignore(char *dst) { | ||
| char str1[5] = {1,2,3,4,5}; | ||
| strncpy(dst, str1, 5); | ||
| strcpy(dst, str1); // expected-warning{{Terminating zero missing from string}} | ||
| } | ||
|
|
||
| size_t test_malloc() { | ||
| char *buf = (char *)malloc(4); | ||
| if (!buf) | ||
| return 0; | ||
| buf[3] = 'a'; | ||
| return strlen(buf); | ||
| } | ||
|
|
||
| extern void f_ext(char *); | ||
| char *g_buf = 0; | ||
|
|
||
| size_t test_escape1() { | ||
| char buf[4] = {1,2,3,4}; | ||
| f_ext(buf); | ||
| return strlen(buf); | ||
| } | ||
|
|
||
| size_t test_escape2(char *x) { | ||
| char buf[4] = {1,2,3,4}; | ||
| g_buf = buf; | ||
| f_ext(x); | ||
| return strlen(buf); | ||
| } | ||
|
|
||
| size_t test_escape3() { | ||
| char buf[4] = {1,2,3,4}; | ||
| f_ext(buf + 3); | ||
| return strlen(buf); | ||
| } | ||
|
|
||
| void test_str_fn(int i, char *dst) { | ||
| char buf[] = {1, 2, 3}; | ||
| switch (i) { | ||
| case 1: | ||
| strcpy(buf, "aa"); // expected-warning{{Terminating zero missing from string}} | ||
| break; | ||
| case 2: | ||
| strcpy(dst, buf); // expected-warning{{Terminating zero missing from string}} | ||
| break; | ||
| case 3: | ||
| strncpy(buf, "aa", 3); | ||
| break; | ||
| case 4: | ||
| strncpy(dst, buf, 3); | ||
| break; | ||
| case 5: | ||
| strcat(buf, "aa"); // expected-warning{{Terminating zero missing from string}} | ||
| break; | ||
| case 6: | ||
| strcat(dst, buf); // expected-warning{{Terminating zero missing from string}} | ||
| break; | ||
| case 7: | ||
| strncat(buf, "aa", 3); // expected-warning{{Terminating zero missing from string}} | ||
| break; | ||
| case 8: | ||
| strncat(dst, buf, 3); | ||
| break; | ||
| } | ||
| } |
Oops, something went wrong.
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.
I can see the code now for the approach discussed at #146664 (comment). Let's continue the discussion there.