diff options
author | Ben Shi <2283975856@qq.com> | 2024-01-16 16:58:07 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-16 16:58:07 +0800 |
commit | 27d963a7089b050d402dbc1ae210d505f7affff6 (patch) | |
tree | 0c1eb20db9ecc254db048fe946908b74834f9aec /clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp | |
parent | f725bb960d45ada3cc4667dd7aa22792d389c7e7 (diff) | |
download | llvm-27d963a7089b050d402dbc1ae210d505f7affff6.zip llvm-27d963a7089b050d402dbc1ae210d505f7affff6.tar.gz llvm-27d963a7089b050d402dbc1ae210d505f7affff6.tar.bz2 |
[clang][analyzer] Improve modeling of 'fseeko' and 'ftello' in StdLibraryFunctionsChecker (#77902)
Diffstat (limited to 'clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp')
-rw-r--r-- | clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp index 3b36565..641ebe9 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp @@ -2220,6 +2220,9 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( 0, WithinRange, {{EOFv, EOFv}, {0, UCharRangeMax}})) .ArgConstraint(NotNull(ArgNo(1)))); + std::optional<QualType> Off_tTy = lookupTy("off_t"); + std::optional<RangeInt> Off_tMax = getMaxValue(Off_tTy); + // int fseek(FILE *stream, long offset, int whence); // FIXME: It can be possible to get the 'SEEK_' values (like EOFv) and use // these for condition of arg 2. @@ -2232,6 +2235,16 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint(NotNull(ArgNo(0))) .ArgConstraint(ArgumentCondition(2, WithinRange, {{0, 2}}))); + // int fseeko(FILE *stream, off_t offset, int whence); + addToFunctionSummaryMap( + "fseeko", + Signature(ArgTypes{FilePtrTy, Off_tTy, IntTy}, RetType{IntTy}), + Summary(NoEvalCall) + .Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg) + .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg) + .ArgConstraint(NotNull(ArgNo(0))) + .ArgConstraint(ArgumentCondition(2, WithinRange, {{0, 2}}))); + // int fgetpos(FILE *restrict stream, fpos_t *restrict pos); // From 'The Open Group Base Specifications Issue 7, 2018 edition': // "The fgetpos() function shall not change the setting of errno if @@ -2279,6 +2292,15 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg) .ArgConstraint(NotNull(ArgNo(0)))); + // off_t ftello(FILE *stream); + addToFunctionSummaryMap( + "ftello", Signature(ArgTypes{FilePtrTy}, RetType{Off_tTy}), + Summary(NoEvalCall) + .Case({ReturnValueCondition(WithinRange, Range(0, Off_tMax))}, + ErrnoMustNotBeChecked, GenericSuccessMsg) + .Case(ReturnsMinusOne, ErrnoNEZeroIrrelevant, GenericFailureMsg) + .ArgConstraint(NotNull(ArgNo(0)))); + // int fileno(FILE *stream); addToFunctionSummaryMap( "fileno", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}), @@ -2410,8 +2432,6 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( .ArgConstraint( ArgumentCondition(0, WithinRange, Range(0, IntMax)))); - std::optional<QualType> Off_tTy = lookupTy("off_t"); - // int truncate(const char *path, off_t length); addToFunctionSummaryMap( "truncate", @@ -2854,19 +2874,6 @@ void StdLibraryFunctionsChecker::initFunctionSummaries( "rand_r", Signature(ArgTypes{UnsignedIntPtrTy}, RetType{IntTy}), Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0)))); - // int fseeko(FILE *stream, off_t offset, int whence); - addToFunctionSummaryMap( - "fseeko", - Signature(ArgTypes{FilePtrTy, Off_tTy, IntTy}, RetType{IntTy}), - Summary(NoEvalCall) - .Case(ReturnsZeroOrMinusOne, ErrnoIrrelevant) - .ArgConstraint(NotNull(ArgNo(0)))); - - // off_t ftello(FILE *stream); - addToFunctionSummaryMap( - "ftello", Signature(ArgTypes{FilePtrTy}, RetType{Off_tTy}), - Summary(NoEvalCall).ArgConstraint(NotNull(ArgNo(0)))); - // void *mmap(void *addr, size_t length, int prot, int flags, int fd, // off_t offset); // FIXME: Improve for errno modeling. |