Skip to content

Commit

Permalink
[clang][analyzer] Improve modeling of 'realpath' in StdLibraryFunctio…
Browse files Browse the repository at this point in the history
…nsChecker (#79939)
  • Loading branch information
benshi001 committed Jan 31, 2024
1 parent 150ab99 commit c12f30c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2992,12 +2992,16 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(

// char *realpath(const char *restrict file_name,
// char *restrict resolved_name);
// FIXME: Improve for errno modeling.
// 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},
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));

Expand Down
10 changes: 10 additions & 0 deletions clang/test/Analysis/errno-stdlibraryfunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
}
}

0 comments on commit c12f30c

Please sign in to comment.