Skip to content

Commit 265c9a6

Browse files
committed
Remove unnecessary nonnull attribute for snprintf and vsnprintf
According to the C standard, the first argument to snprintf and vsnprintf may be null if the size argument is zero.
1 parent 775fcf1 commit 265c9a6

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,7 +3116,7 @@ def FPrintf : LibBuiltin<"stdio.h"> {
31163116

31173117
def SnPrintf : LibBuiltin<"stdio.h"> {
31183118
let Spellings = ["snprintf"];
3119-
let Attributes = [NoThrow, PrintfFormat<2>, NonNull<[0, 2]>];
3119+
let Attributes = [NoThrow, PrintfFormat<2>, NonNull<[2]>];
31203120
let Prototype = "int(char* restrict, size_t, char const* restrict, ...)";
31213121
let AddBuiltinPrefixedAlias = 1;
31223122
}
@@ -3144,7 +3144,7 @@ def VfPrintf : LibBuiltin<"stdio.h"> {
31443144

31453145
def VsnPrintf : LibBuiltin<"stdio.h"> {
31463146
let Spellings = ["vsnprintf"];
3147-
let Attributes = [NoThrow, VPrintfFormat<2>, NonNull<[0, 2]>];
3147+
let Attributes = [NoThrow, VPrintfFormat<2>, NonNull<[2]>];
31483148
let Prototype = "int(char* restrict, size_t, char const* restrict, __builtin_va_list)";
31493149
let AddBuiltinPrefixedAlias = 1;
31503150
}

clang/test/Sema/format-strings-nonnull.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ void check_format_string(FILE *fp, va_list ap) {
4141
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
4242
// expected-warning@-2{{null passed to a callee that requires a non-null argument}}
4343

44-
snprintf(NULL, 10, 0, 42);
44+
snprintf(buf, 10, 0, 42);
4545
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
46-
// expected-warning@-2{{null passed to a callee that requires a non-null argument}}
4746

4847
vprintf(fmt, ap);
4948
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
@@ -54,9 +53,8 @@ void check_format_string(FILE *fp, va_list ap) {
5453
vsprintf(buf, nullptr, ap);
5554
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
5655

57-
vsnprintf(NULL, 10, fmt, ap);
56+
vsnprintf(buf, 10, fmt, ap);
5857
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}
59-
// expected-warning@-2{{null passed to a callee that requires a non-null argument}}
6058

6159
scanf(NULL);
6260
// expected-warning@-1{{null passed to a callee that requires a non-null argument}}

0 commit comments

Comments
 (0)