[clang] [clang][analyzer] Improve modeling of 'realpath' in StdLibraryFunctionsChecker (PR #79939)
Ben Shi via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 30 16:03:16 PST 2024
https://github.com/benshi001 updated https://github.com/llvm/llvm-project/pull/79939
>From 5bbac365c3b587bc72f503c4ce146503f0d0095f Mon Sep 17 00:00:00 2001
From: Ben Shi <bennshi at tencent.com>
Date: Tue, 30 Jan 2024 11:45:30 +0800
Subject: [PATCH 1/2] [clang][analyzer] Improve modeling of 'realpath' in
StdLibraryFunctionsChecker
---
.../Checkers/StdLibraryFunctionsChecker.cpp | 6 ++++--
clang/test/Analysis/errno-stdlibraryfunctions.c | 10 ++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index be26f5521c8d7..3bcde86c9e66f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -2992,12 +2992,14 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
// char *realpath(const char *restrict file_name,
// char *restrict resolved_name);
- // FIXME: Improve for errno modeling.
addToFunctionSummaryMap(
"realpath",
Signature(ArgTypes{ConstCharPtrRestrictTy, CharPtrRestrictTy},
RetType{CharPtrTy}),
- Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0))));
+ Summary(NoEvalCall)
+ .Case({NotNull(Ret)}, ErrnoMustNotBeChecked, GenericSuccessMsg)
+ .Case({IsNull(Ret)}, ErrnoNEZeroIrrelevant, GenericFailureMsg)
+ .ArgConstraint(NotNull(ArgNo(0))));
QualType CharPtrConstPtr = getPointerTy(getConstTy(CharPtrTy));
diff --git a/clang/test/Analysis/errno-stdlibraryfunctions.c b/clang/test/Analysis/errno-stdlibraryfunctions.c
index 9b487fed0a2eb..a28efb764edfd 100644
--- a/clang/test/Analysis/errno-stdlibraryfunctions.c
+++ b/clang/test/Analysis/errno-stdlibraryfunctions.c
@@ -128,3 +128,13 @@ void errno_pclose(void) {
if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
}
}
+
+void errno_realpath(char *Path, char *Buf) {
+ char *Ret = realpath(Path, Buf);
+ if (!Ret) {
+ clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
+ if (errno) {} // no-warning
+ } else {
+ if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
+ }
+}
>From 8a32d3d5bc8df658f8efef0ca2175292177032f4 Mon Sep 17 00:00:00 2001
From: Ben Shi <bennshi at tencent.com>
Date: Wed, 31 Jan 2024 08:02:42 +0800
Subject: [PATCH 2/2] [clang][analyzer] Improve modeling of 'realpath' in
StdLibraryFunctionsChecker
---
.../lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
index 3bcde86c9e66f..0c6293e67a86f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
@@ -2992,6 +2992,8 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
// char *realpath(const char *restrict file_name,
// char *restrict resolved_name);
+ // FIXME: If the argument 'resolved_name' is not NULL, macro 'PATH_MAX'
+ // should be defined in "limits.h" to guarrantee a success.
addToFunctionSummaryMap(
"realpath",
Signature(ArgTypes{ConstCharPtrRestrictTy, CharPtrRestrictTy},
More information about the cfe-commits
mailing list