[clang] [-Wunsafe-buffer-usage] Fix crashes in "Add check for custom printf/scanf functions #173096" (PR #174683)
Ziqing Luo via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 7 13:15:22 PST 2026
================
@@ -280,11 +307,65 @@ void test_myprintf(char * Str, std::string StdStr) {
myprintf_3("irrelevant", "hello %s", 0, StdStr.c_str());
myprintf_3("irrelevant", "hello %s", 0, Str); // expected-warning{{function 'myprintf_3' is unsafe}} \
expected-note{{string argument is not guaranteed to be null-terminated}}
-
myscanf("hello %s");
myscanf("hello %s", Str); // expected-warning{{function 'myscanf' is unsafe}}
+ myprintf_default("irrelevant");
+
int X;
myscanf("hello %d", &X); // expected-warning{{function 'myscanf' is unsafe}}
+
+ // Test member functions:
+ FormatAttrTestMember Obj;
+
+ Obj.myprintf("hello", Str);
+ Obj.myprintf("hello %s", StdStr.c_str());
+ Obj.myprintf("hello %s", Str); // expected-warning{{function 'myprintf' is unsafe}} \
+ expected-note{{string argument is not guaranteed to be null-terminated}}
+
+ Obj.myprintf_2("hello", 0, Str);
+ Obj.myprintf_2("hello %s", 0, StdStr.c_str());
+ Obj.myprintf_2("hello %s", 0, Str); // expected-warning{{function 'myprintf_2' is unsafe}} \
+ expected-note{{string argument is not guaranteed to be null-terminated}}
+
+ Obj.myprintf_3("irrelevant", "hello", 0, Str);
+ Obj.myprintf_3("irrelevant", "hello %s", 0, StdStr.c_str());
+ Obj.myprintf_3("irrelevant", "hello %s", 0, Str); // expected-warning{{function 'myprintf_3' is unsafe}} \
+ expected-note{{string argument is not guaranteed to be null-terminated}}
+
+ Obj.myscanf("hello %s");
+ Obj.myscanf("hello %s", Str); // expected-warning{{function 'myscanf' is unsafe}}
+
+ Obj.myscanf("hello %d", &X); // expected-warning{{function 'myscanf' is unsafe}}
+
+ Obj.myprintf_default("irrelevant"); // expected-warning{{function 'myprintf_default' is unsafe}}
+ // expected-note@*{{string argument is not guaranteed to be null-terminated}}
----------------
ziqingluo-90 wrote:
@ojhunt one more mild irritating fact: CXXDefaultArgExpr has no valid `BeginLoc` ðŸ«
https://github.com/llvm/llvm-project/pull/174683
More information about the cfe-commits
mailing list