llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-analysis

Author: Hans Wennborg (zmodem)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/175208.diff


2 Files Affected:

- (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+5) 
- (modified) clang/test/SemaCXX/warn-unsafe-buffer-usage-libc-functions.cpp 
(+4) 


``````````diff
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}} \

``````````

</details>


https://github.com/llvm/llvm-project/pull/175208
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to