Skip to content

Commit 925ff9e

Browse files
benshi001steakhal
andauthored
[clang][analyzer] Support 'fflush' in the StdLibraryFunctionsChecker (#76557)
Co-authored-by: Balazs Benics <[email protected]>
1 parent 69bc371 commit 925ff9e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1126,9 +1126,11 @@ Improvements
11261126
^^^^^^^^^^^^
11271127

11281128
- Improved the ``unix.StdCLibraryFunctions`` checker by modeling more
1129-
functions like ``send``, ``recv``, ``readlink`` and ``errno`` behavior.
1129+
functions like ``send``, ``recv``, ``readlink``, ``fflush`` and
1130+
``errno`` behavior.
11301131
(`52ac71f92d38 <https://github.com/llvm/llvm-project/commit/52ac71f92d38f75df5cb88e9c090ac5fd5a71548>`_,
11311132
`#71373 <https://github.com/llvm/llvm-project/pull/71373>`_,
1133+
`#76557 <https://github.com/llvm/llvm-project/pull/76557>`_,
11321134
`#71392 <https://github.com/llvm/llvm-project/pull/71392>`_)
11331135

11341136
- Fixed a false negative for when accessing a nonnull property (ObjC).

clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,6 +2244,14 @@ void StdLibraryFunctionsChecker::initFunctionSummaries(
22442244
.ArgConstraint(NotNull(ArgNo(0)))
22452245
.ArgConstraint(NotNull(ArgNo(1))));
22462246

2247+
// int fflush(FILE *stream);
2248+
addToFunctionSummaryMap(
2249+
"fflush", Signature(ArgTypes{FilePtrTy}, RetType{IntTy}),
2250+
Summary(NoEvalCall)
2251+
.Case(ReturnsZero, ErrnoMustNotBeChecked, GenericSuccessMsg)
2252+
.Case({ReturnValueCondition(WithinRange, SingleValue(EOFv))},
2253+
ErrnoNEZeroIrrelevant, GenericFailureMsg));
2254+
22472255
// long ftell(FILE *stream);
22482256
// From 'The Open Group Base Specifications Issue 7, 2018 edition':
22492257
// "The ftell() function shall not change the setting of errno if

clang/test/Analysis/stream-errno.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,29 @@ void check_fileno(void) {
222222
}
223223
if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
224224
}
225+
226+
void check_fflush_opened_file(void) {
227+
FILE *F = tmpfile();
228+
if (!F)
229+
return;
230+
int N = fflush(F);
231+
if (N == EOF) {
232+
clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
233+
if (errno) {} // no-warning
234+
} else {
235+
clang_analyzer_eval(N == 0); // expected-warning{{TRUE}}
236+
if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
237+
}
238+
fclose(F);
239+
}
240+
241+
void check_fflush_all(void) {
242+
int N = fflush(NULL);
243+
if (N == 0) {
244+
if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
245+
} else {
246+
clang_analyzer_eval(N == EOF); // expected-warning{{TRUE}}
247+
clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
248+
if (errno) {} // no-warning
249+
}
250+
}

0 commit comments

Comments
 (0)