Skip to content

Commit dbfef7f

Browse files
Validate that format strings match when both caller and callee have format_matches
1 parent 29fa8eb commit dbfef7f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6085,8 +6085,8 @@ static StringLiteralCheckType checkFormatStringExpr(
60856085
continue;
60866086
if (PV->getFunctionScopeIndex() == CallerFSI.FormatIdx)
60876087
return checkFormatStringExpr(
6088-
S, nullptr, PVFormatMatches->getFormatString(), Args, APK,
6089-
format_idx, firstDataArg, Type, CallType,
6088+
S, ReferenceFormatString, PVFormatMatches->getFormatString(),
6089+
Args, APK, format_idx, firstDataArg, Type, CallType,
60906090
/*InFunctionCall*/ false, CheckedVarArgs, UncoveredArg,
60916091
Offset, IgnoreStringsWithoutSpecifiers);
60926092
}

clang/test/Sema/format-string-matches.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,19 @@ void test_freebsd_specifiers(void) {
105105
cvt_freebsd_D("%b");
106106
cvt_freebsd_D("%s %i"); // expected-warning{{format argument is a value, but it should be an auxiliary value}}
107107
}
108+
109+
__attribute__((format_matches(printf, 1, "%s"))) // expected-note{{comparing with this specifier}}
110+
__attribute__((format_matches(os_log, 2, "%i"))) // expected-note{{comparing with this specifier}}
111+
void test_recv_multiple_format_strings(const char *fmt1, const char *fmt2);
112+
113+
__attribute__((format_matches(printf, 1, "%s")))
114+
__attribute__((format_matches(os_log, 2, "%i")))
115+
void test_multiple_format_strings(const char *fmt1, const char *fmt2) {
116+
test_recv_multiple_format_strings("%s", "%i");
117+
test_recv_multiple_format_strings("%s", "%s"); // expected-warning{{format specifier 's' is incompatible with 'i'}}
118+
test_recv_multiple_format_strings("%i", "%i"); // expected-warning{{format specifier 'i' is incompatible with 's'}}
119+
120+
test_recv_multiple_format_strings(fmt1, fmt2);
121+
test_recv_multiple_format_strings("%.5s", fmt2);
122+
test_recv_multiple_format_strings(fmt1, "%04d");
123+
}

0 commit comments

Comments
 (0)