Skip to content

Commit 039952f

Browse files
committed
Add NotNull constaints to arg 0 for the summary Cases
1 parent c2badaa commit 039952f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,13 +2651,16 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
26512651
addToFunctionSummaryMap(
26522652
"getcwd", Signature(ArgTypes{CharPtrTy, SizeTy}, RetType{CharPtrTy}),
26532653
Summary(NoEvalCall)
2654-
.Case({ArgumentCondition(1, WithinRange, Range(1, SizeMax)),
2654+
.Case({NotNull(ArgNo(0)),
2655+
ArgumentCondition(1, WithinRange, Range(1, SizeMax)),
26552656
ReturnValueCondition(BO_EQ, ArgNo(0))},
26562657
ErrnoMustNotBeChecked, GenericSuccessMsg)
2657-
.Case({ArgumentCondition(1, WithinRange, SingleValue(0)),
2658+
.Case({NotNull(ArgNo(0)),
2659+
ArgumentCondition(1, WithinRange, SingleValue(0)),
26582660
IsNull(Ret)},
26592661
ErrnoNEZeroIrrelevant, "Assuming that argument 'size' is 0")
2660-
.Case({ArgumentCondition(1, WithinRange, Range(1, SizeMax)),
2662+
.Case({NotNull(ArgNo(0)),
2663+
ArgumentCondition(1, WithinRange, Range(1, SizeMax)),
26612664
IsNull(Ret)},
26622665
ErrnoNEZeroIrrelevant, GenericFailureMsg)
26632666
.ArgConstraint(

clang/test/Analysis/errno-stdlibraryfunctions.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "Inputs/errno_var.h"
1010
#include "Inputs/std-c-library-functions-POSIX.h"
11+
#include "Inputs/system-header-simulator-for-malloc.h"
1112

1213
#define NULL ((void *) 0)
1314

@@ -105,12 +106,21 @@ void errno_getcwd(char *Buf, size_t Sz) {
105106
clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
106107
clang_analyzer_eval(Path == NULL); // expected-warning{{TRUE}}
107108
if (errno) {} // no warning
109+
} else if (Path == NULL) {
110+
clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
111+
if (errno) {} // no warning
108112
} else {
109113
clang_analyzer_eval(Path == Buf); // expected-warning{{TRUE}}
110114
if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
111115
}
112116
}
113117

118+
void gh_128882_getcwd(void) {
119+
// We expect no warnings here.
120+
char* currentPath = getcwd(NULL, 0);
121+
free(currentPath);
122+
}
123+
114124
void errno_execv(char *Path, char * Argv[]) {
115125
int Ret = execv(Path, Argv);
116126
clang_analyzer_eval(Ret == -1); // expected-warning{{TRUE}}

0 commit comments

Comments
 (0)