https://github.com/benshi001 created https://github.com/llvm/llvm-project/pull/79939
None >From 5bbac365c3b587bc72f503c4ce146503f0d0095f Mon Sep 17 00:00:00 2001 From: Ben Shi <benn...@tencent.com> Date: Tue, 30 Jan 2024 11:45:30 +0800 Subject: [PATCH] [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'}} + } +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits