-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang][sema] Add nonnull attribute to builtin format functions #160988
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
bozicrHT
wants to merge
1
commit into
llvm:main
Choose a base branch
from
bozicrHT:sema-builtin-nonnull
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.
+183
−36
Open
Changes from all commits
Commits
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
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,74 @@ | ||
// RUN: %clang_cc1 -fsyntax-only --std=c23 -verify -Wnonnull -Wno-format-security %s | ||
|
||
#define NULL (void*)0 | ||
|
||
typedef struct _FILE FILE; | ||
typedef __SIZE_TYPE__ size_t; | ||
typedef __builtin_va_list va_list; | ||
int printf(char const* restrict, ...); | ||
int __builtin_printf(char const* restrict, ...); | ||
int fprintf(FILE* restrict, char const* restrict, ...); | ||
int snprintf(char* restrict, size_t, char const* restrict, ...); | ||
int sprintf(char* restrict, char const* restrict, ...); | ||
int vprintf(char const* restrict, __builtin_va_list); | ||
int vfprintf(FILE* restrict, char const* restrict, __builtin_va_list); | ||
int vsnprintf(char* restrict, size_t, char const* restrict, __builtin_va_list); | ||
int vsprintf(char* restrict, char const* restrict, __builtin_va_list); | ||
|
||
int scanf(char const* restrict, ...); | ||
int fscanf(FILE* restrict, char const* restrict, ...); | ||
int sscanf(char const* restrict, char const* restrict, ...); | ||
int vscanf(char const* restrict, __builtin_va_list); | ||
int vfscanf(FILE* restrict, char const* restrict, __builtin_va_list); | ||
int vsscanf(char const* restrict, char const* restrict, __builtin_va_list); | ||
|
||
|
||
void check_format_string(FILE *fp, va_list ap) { | ||
char buf[256]; | ||
char* const fmt = NULL; | ||
|
||
printf(fmt); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
__builtin_printf(NULL, "xxd"); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
fprintf(fp, NULL, 25); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
sprintf(buf, NULL, 42); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
snprintf(buf, 10, 0, 42); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
vprintf(fmt, ap); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
vfprintf(fp, 0, ap); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
vsprintf(buf, nullptr, ap); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
vsnprintf(buf, 10, fmt, ap); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
scanf(NULL); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
fscanf(fp, nullptr); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
sscanf(buf, fmt); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
vscanf(NULL, ap); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
vfscanf(fp, fmt, ap); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
|
||
vsscanf(buf, NULL, ap); | ||
// expected-warning@-1{{null passed to a callee that requires a non-null argument}} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. smallest nit: newline |
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,40 @@ | ||
// RUN: %clang_cc1 -fsyntax-only -verify -Wnonnull %s | ||
|
||
#ifdef __cplusplus | ||
# define EXTERN_C extern "C" | ||
#else | ||
# define EXTERN_C extern | ||
#endif | ||
|
||
typedef struct _FILE FILE; | ||
typedef __SIZE_TYPE__ size_t; | ||
typedef __builtin_va_list va_list; | ||
|
||
EXTERN_C int printf(const char *, ...); | ||
EXTERN_C int fprintf(FILE *, const char *restrict, ...); | ||
EXTERN_C int sprintf(char* restrict, char const* res, ...); | ||
|
||
EXTERN_C int scanf(char const *restrict, ...); | ||
EXTERN_C int fscanf(FILE* restrict, char const* res, ...); | ||
|
||
void test(FILE *fp) { | ||
char buf[256]; | ||
|
||
__builtin_printf(__null, "x"); | ||
// expected-warning@-1 {{null passed to a callee that requires a non-null argument}} | ||
|
||
printf(__null, "xxd"); | ||
// expected-warning@-1 {{null passed to a callee that requires a non-null argument}} | ||
|
||
fprintf(fp, __null, 42); | ||
// expected-warning@-1 {{null passed to a callee that requires a non-null argument}} | ||
|
||
sprintf(buf, __null); | ||
// expected-warning@-1 {{null passed to a callee that requires a non-null argument}} | ||
|
||
scanf(__null); | ||
// expected-warning@-1 {{null passed to a callee that requires a non-null argument}} | ||
|
||
fscanf(fp, __null); | ||
// expected-warning@-1 {{null passed to a callee that requires a non-null argument}} | ||
} |
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
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.
Shouldn't the
hasAttr<NonNullAttr>()
check be per parameter?This reads as if
Would not get the attribute annotation, and similarly
would have the attribute added again?