From 7fc150309d27b0f98239bec758b895efda8c0481 Mon Sep 17 00:00:00 2001 From: Balazs Benics Date: Wed, 13 Oct 2021 10:50:26 +0200 Subject: [PATCH] [analyzer] Bifurcate on getenv() calls The `getenv()` function might return `NULL` just like any other function. However, in case of `getenv()` a state-split seems justified since the programmer should expect the failure of this function. `secure_getenv(const char *name)` behaves the same way but is not handled right now. Note that `std::getenv()` is also not handled. Reviewed By: martong Differential Revision: https://reviews.llvm.org/D111245 --- .../Checkers/StdLibraryFunctionsChecker.cpp | 8 ++++++++ clang/test/Analysis/std-c-library-functions.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index e758b465af1b..74adc5882bfb 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -1433,6 +1433,14 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( RetType{Ssize_tTy}), GetLineSummary); + // char *getenv(const char *name); + addToFunctionSummaryMap( + "getenv", Signature(ArgTypes{ConstCharPtrTy}, RetType{CharPtrTy}), + Summary(NoEvalCall) + .Case({NotNull(Ret)}) + .Case({NotNull(Ret)->negate()}) + .ArgConstraint(NotNull(ArgNo(0)))); + if (ModelPOSIX) { // long a64l(const char *str64); diff --git a/clang/test/Analysis/std-c-library-functions.c b/clang/test/Analysis/std-c-library-functions.c index 9288af9d43b8..e1800ed390a8 100644 --- a/clang/test/Analysis/std-c-library-functions.c +++ b/clang/test/Analysis/std-c-library-functions.c @@ -254,3 +254,11 @@ void test_call_by_pointer() { f = ispunct; clang_analyzer_eval(f('A')); // expected-warning{{FALSE}} } + +char *getenv(const char *name); +void test_getenv() { + // getenv() bifurcates here. + clang_analyzer_eval(getenv("FOO") == 0); + // expected-warning@-1 {{TRUE}} + // expected-warning@-2 {{FALSE}} +}