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}} +}