-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][analyzer] Fix argument invalidations in StreamChecker. #79470
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
Merged
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// RUN: %clang_analyze_cc1 -verify %s \ | ||
// RUN: -analyzer-checker=core \ | ||
// RUN: -analyzer-checker=alpha.unix.Stream \ | ||
// RUN: -analyzer-checker=debug.ExprInspection | ||
|
||
#include "Inputs/system-header-simulator.h" | ||
|
||
void clang_analyzer_eval(int); | ||
void clang_analyzer_dump(int); | ||
|
||
void test_fread(void) { | ||
FILE *F = fopen("file", "r+"); | ||
if (!F) | ||
return; | ||
|
||
char Buf[3] = {10, 10, 10}; | ||
fread(Buf, 1, 3, F); | ||
// The check applies to success and failure. | ||
clang_analyzer_dump(Buf[0]); // expected-warning {{conj_$}} Should not preserve the previous value, thus should not be 10. | ||
clang_analyzer_dump(Buf[2]); // expected-warning {{conj_$}} | ||
if (feof(F)) { | ||
steakhal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
char Buf1[3] = {10, 10, 10}; | ||
fread(Buf1, 1, 3, F); // expected-warning {{is in EOF state}} | ||
clang_analyzer_dump(Buf1[0]); // expected-warning {{10 S32b}} | ||
clang_analyzer_dump(Buf1[2]); // expected-warning {{10 S32b}} | ||
} | ||
|
||
fclose(F); | ||
} | ||
|
||
void test_fwrite(void) { | ||
FILE *F = fopen("file", "r+"); | ||
if (!F) | ||
return; | ||
|
||
char Buf[3] = {10, 10, 10}; | ||
fwrite(Buf, 1, 3, F); | ||
// The check applies to success and failure. | ||
clang_analyzer_dump(Buf[0]); // expected-warning {{10 S32b}} | ||
clang_analyzer_dump(Buf[2]); // expected-warning {{10 S32b}} | ||
|
||
fclose(F); | ||
} | ||
|
||
void test_fgets() { | ||
FILE *F = tmpfile(); | ||
if (!F) | ||
return; | ||
|
||
char Buf[3] = {10, 10, 10}; | ||
fgets(Buf, 3, F); | ||
// The check applies to success and failure. | ||
clang_analyzer_dump(Buf[0]); // expected-warning {{conj_$}} Should not preserve the previous value, thus should not be 10. | ||
clang_analyzer_dump(Buf[2]); // expected-warning {{conj_$}} | ||
if (feof(F)) { | ||
char Buf1[3] = {10, 10, 10}; | ||
fgets(Buf1, 3, F); // expected-warning {{is in EOF state}} | ||
clang_analyzer_dump(Buf1[0]); // expected-warning {{10 S32b}} | ||
clang_analyzer_dump(Buf1[2]); // expected-warning {{10 S32b}} | ||
} | ||
|
||
fclose(F); | ||
} | ||
|
||
void test_fputs() { | ||
FILE *F = tmpfile(); | ||
if (!F) | ||
return; | ||
|
||
char *Buf = "aaa"; | ||
fputs(Buf, F); | ||
// The check applies to success and failure. | ||
clang_analyzer_dump(Buf[0]); // expected-warning {{97 S32b}} | ||
clang_analyzer_dump(Buf[2]); // expected-warning {{97 S32b}} | ||
clang_analyzer_dump(Buf[3]); // expected-warning {{0 S32b}} | ||
|
||
fclose(F); | ||
} | ||
|
||
void test_fscanf() { | ||
FILE *F = tmpfile(); | ||
if (!F) | ||
return; | ||
|
||
int a = 1; | ||
unsigned b; | ||
int Ret = fscanf(F, "%d %u", &a, &b); | ||
if (Ret == 0) { | ||
clang_analyzer_dump(a); // expected-warning {{conj_$}} | ||
// FIXME: should be {{1 S32b}}. | ||
clang_analyzer_dump(b); // expected-warning {{conj_$}} | ||
// FIXME: should be {{uninitialized value}}. | ||
} else if (Ret == 1) { | ||
clang_analyzer_dump(a); // expected-warning {{conj_$}} | ||
clang_analyzer_dump(b); // expected-warning {{conj_$}} | ||
// FIXME: should be {{uninitialized value}}. | ||
} else if (Ret >= 2) { | ||
clang_analyzer_dump(a); // expected-warning {{conj_$}} | ||
steakhal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clang_analyzer_dump(b); // expected-warning {{conj_$}} | ||
clang_analyzer_eval(Ret == 2); // expected-warning {{FALSE}} expected-warning {{TRUE}} | ||
// FIXME: should be only TRUE. | ||
} else { | ||
clang_analyzer_dump(a); // expected-warning {{1 S32b}} | ||
clang_analyzer_dump(b); // expected-warning {{uninitialized value}} | ||
} | ||
|
||
fclose(F); | ||
} | ||
|
||
void test_getdelim(char *P, size_t Sz) { | ||
FILE *F = tmpfile(); | ||
if (!F) | ||
return; | ||
|
||
char *P1 = P; | ||
size_t Sz1 = Sz; | ||
ssize_t Ret = getdelim(&P, &Sz, '\t', F); | ||
if (Ret < 0) { | ||
clang_analyzer_eval(P == P1); // expected-warning {{FALSE}} \ | ||
// expected-warning {{TRUE}} | ||
clang_analyzer_eval(Sz == Sz1); // expected-warning {{FALSE}} \ | ||
// expected-warning {{TRUE}} | ||
} else { | ||
clang_analyzer_eval(P == P1); // expected-warning {{FALSE}} \ | ||
// expected-warning {{TRUE}} | ||
clang_analyzer_eval(Sz == Sz1); // expected-warning {{FALSE}} \ | ||
// expected-warning {{TRUE}} | ||
} | ||
|
||
fclose(F); | ||
} | ||
|
||
void test_fgetpos() { | ||
FILE *F = tmpfile(); | ||
if (!F) | ||
return; | ||
|
||
fpos_t Pos = 1; | ||
int Ret = fgetpos(F, &Pos); | ||
if (Ret == 0) { | ||
clang_analyzer_dump(Pos); // expected-warning {{conj_$}} | ||
} else { | ||
clang_analyzer_dump(Pos); // expected-warning {{1 S32b}} | ||
} | ||
|
||
fclose(F); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.