[clang] 3db749a - [clang][analyzer] Improve 'errno' modeling of 'mkdtemp' (#76671)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 3 02:23:49 PST 2024
Author: Ben Shi
Date: 2024-01-03T18:23:45+08:00
New Revision: 3db749afcb5079dac332942a3e0b258cdb629a02
URL: https://github.com/llvm/llvm-project/commit/3db749afcb5079dac332942a3e0b258cdb629a02
DIFF: https://github.com/llvm/llvm-project/commit/3db749afcb5079dac332942a3e0b258cdb629a02.diff
LOG: [clang][analyzer] Improve 'errno' modeling of 'mkdtemp' (#76671)
Added:
Modified:
clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
clang/test/Analysis/errno-stdlibraryfunctions.c
Removed:
################################################################################
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index 6560fd239ce668..20068653d530a3 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -2507,10 +2507,13 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
.ArgConstraint(NotNull(ArgNo(0))));
// char *mkdtemp(char *template);
- // FIXME: Improve for errno modeling.
addToFunctionSummaryMap(
"mkdtemp", Signature(ArgTypes{CharPtrTy}, RetType{CharPtrTy}),
- Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
+ Summary(NoEvalCall)
+ .Case({ReturnValueCondition(BO_EQ, ArgNo(0))},
+ ErrnoMustNotBeChecked, GenericSuccessMsg)
+ .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
+ .ArgConstraint(NotNull(ArgNo(0))));
// char *getcwd(char *buf, size_t size);
// FIXME: Improve for errno modeling.
diff --git a/clang/test/Analysis/errno-stdlibraryfunctions.c b/clang/test/Analysis/errno-stdlibraryfunctions.c
index dafda764af9f38..80e14c4e2923ca 100644
--- a/clang/test/Analysis/errno-stdlibraryfunctions.c
+++ b/clang/test/Analysis/errno-stdlibraryfunctions.c
@@ -7,12 +7,9 @@
// RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true
#include "Inputs/errno_var.h"
+#include "Inputs/std-c-library-functions-POSIX.h"
-typedef typeof(sizeof(int)) size_t;
-typedef __typeof(sizeof(int)) off_t;
-typedef size_t ssize_t;
-ssize_t send(int sockfd, const void *buf, size_t len, int flags);
-off_t lseek(int fildes, off_t offset, int whence);
+#define NULL ((void *) 0)
void clang_analyzer_warnIfReached();
void clang_analyzer_eval(int);
@@ -54,3 +51,26 @@ int errno_lseek(int fildes, off_t offset) {
}
return 0;
}
+
+void errno_mkstemp(char *template) {
+ int FD = mkstemp(template);
+ if (FD >= 0) {
+ if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
+ close(FD);
+ } else {
+ clang_analyzer_eval(FD == -1); // expected-warning{{TRUE}}
+ clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
+ if (errno) {} // no warning
+ }
+}
+
+void errno_mkdtemp(char *template) {
+ char *Dir = mkdtemp(template);
+ if (Dir == NULL) {
+ clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
+ if (errno) {} // no warning
+ } else {
+ clang_analyzer_eval(Dir == template); // expected-warning{{TRUE}}
+ if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
+ }
+}
More information about the cfe-commits
mailing list