[clang] [clang][analyzer] Improve modeling of 'execv' and 'execvp' in StdLibraryFunctionsChecker (PR #78930)

Ben Shi via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 24 02:33:36 PST 2024


https://github.com/benshi001 updated https://github.com/llvm/llvm-project/pull/78930

>From 74a43113ee4152eb0cb6aaeb4fbc917bcf0abcf8 Mon Sep 17 00:00:00 2001
From: Ben Shi <bennshi at tencent.com>
Date: Mon, 22 Jan 2024 12:42:43 +0800
Subject: [PATCH 1/2] [clang[analyzer] Improve modeling of 'execv' and 'execvp'
 in StdLibraryFunctionsChecker

These functions always return -1 and set 'errno'.
---
 .../StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index fcd907a9bb0da0..61bf3c8528be2b 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -3002,7 +3002,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
         "execv",
         Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
         Summary(NoEvalCall)
-            .Case(ReturnsMinusOne, ErrnoIrrelevant)
+            .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant)
             .ArgConstraint(NotNull(ArgNo(0))));
 
     // int execvp(const char *file, char *const argv[]);
@@ -3010,7 +3010,7 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
         "execvp",
         Signature(ArgTypes{ConstCharPtrTy, CharPtrConstPtr}, RetType{IntTy}),
         Summary(NoEvalCall)
-            .Case(ReturnsMinusOne, ErrnoIrrelevant)
+            .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant)
             .ArgConstraint(NotNull(ArgNo(0))));
 
     // int getopt(int argc, char * const argv[], const char *optstring);

>From e18e2c0318f4582b071e043183cdc874e034cbc2 Mon Sep 17 00:00:00 2001
From: Ben Shi <bennshi at tencent.com>
Date: Wed, 24 Jan 2024 18:32:54 +0800
Subject: [PATCH 2/2] [clang[analyzer] Improve modeling of 'execv' and 'execvp'
 in StdLibraryFunctionsChecker

These functions always return -1 and set 'errno'.
---
 clang/test/Analysis/errno-stdlibraryfunctions.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/clang/test/Analysis/errno-stdlibraryfunctions.c b/clang/test/Analysis/errno-stdlibraryfunctions.c
index 9e3d07e7aa88a0..7876bafc2eb210 100644
--- a/clang/test/Analysis/errno-stdlibraryfunctions.c
+++ b/clang/test/Analysis/errno-stdlibraryfunctions.c
@@ -89,3 +89,17 @@ void errno_getcwd(char *Buf, size_t Sz) {
     if (errno) {}                      // expected-warning{{An undefined value may be read from 'errno'}}
   }
 }
+
+void errno_execv(char *Path, char * Argv[]) {
+  int Ret = execv(Path, Argv);
+  clang_analyzer_eval(Ret == -1);  // expected-warning{{TRUE}}
+  clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
+  if (errno) {}                    // no warning
+}
+
+void errno_execvp(char *File, char * Argv[]) {
+  int Ret = execvp(File, Argv);
+  clang_analyzer_eval(Ret == -1);  // expected-warning{{TRUE}}
+  clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
+  if (errno) {}                    // no warning
+}



More information about the cfe-commits mailing list