https://github.com/zmodem created https://github.com/llvm/llvm-project/pull/175208
Passing strerror(errno) to printf of printf-like logging functions is a common pattern, and strerror() returns a null-terminated string. Follow-up to #173096 >From 39dcae4284d0d83ce236f3c2be24e0b71a3da294 Mon Sep 17 00:00:00 2001 From: Hans Wennborg <[email protected]> Date: Fri, 9 Jan 2026 18:23:57 +0100 Subject: [PATCH] [-Wunsafe-buffer-usage] Allow strerror for %s in printf-like calls Passing strerror(errno) to printf of printf-like logging functions is a common pattern, and strerror() returns a null-terminated string. Follow-up to #173096 --- clang/lib/Analysis/UnsafeBufferUsage.cpp | 5 +++++ .../test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp index 7c0eaa2e589f5..4614a586565cb 100644 --- a/clang/lib/Analysis/UnsafeBufferUsage.cpp +++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp @@ -771,6 +771,11 @@ static bool isNullTermPointer(const Expr *Ptr, ASTContext &Ctx) { if (MD->getName() == "c_str" && RD->getName() == "basic_string") return true; } + if (auto *CE = dyn_cast<CallExpr>(Ptr->IgnoreParenImpCasts())) { + const FunctionDecl *F = CE->getDirectCallee(); + if (F && F->getName() == "strerror") + return true; + } return false; } diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp index d824463ad9618..fe9bc7c809c96 100644 --- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp +++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp @@ -298,6 +298,10 @@ void test_format_attr(char * Str, std::string StdStr) { myprintf("hello %s", Str); // expected-warning{{function 'myprintf' is unsafe}} \ expected-note{{string argument is not guaranteed to be null-terminated}} + extern int errno; + extern char *strerror(int errnum); + myprintf("errno: %s", strerror(errno)); + myprintf_2("hello", 0, Str); myprintf_2("hello %s", 0, StdStr.c_str()); myprintf_2("hello %s", 0, Str); // expected-warning{{function 'myprintf_2' is unsafe}} \ _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
