diff --git a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp index 69610029beb0a..df0865e85eaef 100644 --- a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp @@ -245,11 +245,14 @@ class StreamChecker : public CheckerStreamArgNo), C, @@ -650,7 +656,7 @@ void StreamChecker::preFreadFwrite(const FnDescription *Desc, if (!State) return; - if (!IsFread) { + if (!IsRead) { C.addTransition(State); return; } @@ -745,6 +751,45 @@ void StreamChecker::evalFreadFwrite(const FnDescription *Desc, C.addTransition(StateFailed); } +void StreamChecker::evalFputc(const FnDescription *Desc, const CallEvent &Call, + CheckerContext &C) const { + ProgramStateRef State = C.getState(); + SymbolRef StreamSym = getStreamArg(Desc, Call).getAsSymbol(); + if (!StreamSym) + return; + + const CallExpr *CE = dyn_cast_or_null(Call.getOriginExpr()); + if (!CE) + return; + + const StreamState *OldSS = State->get(StreamSym); + if (!OldSS) + return; + + assertStreamStateOpened(OldSS); + + // `fputc` returns the written character on success, otherwise returns EOF. + + // Generate a transition for the success state. + std::optional PutVal = Call.getArgSVal(0).getAs(); + if (!PutVal) + return; + ProgramStateRef StateNotFailed = + State->BindExpr(CE, C.getLocationContext(), *PutVal); + StateNotFailed = + StateNotFailed->set(StreamSym, StreamState::getOpened(Desc)); + C.addTransition(StateNotFailed); + + // Add transition for the failed state. + // If a (non-EOF) error occurs, the resulting value of the file position + // indicator for the stream is indeterminate. + ProgramStateRef StateFailed = bindInt(*EofVal, State, C, CE); + StreamState NewSS = StreamState::getOpened( + Desc, ErrorFError, /*IsFilePositionIndeterminate*/ true); + StateFailed = StateFailed->set(StreamSym, NewSS); + C.addTransition(StateFailed); +} + void StreamChecker::preFseek(const FnDescription *Desc, const CallEvent &Call, CheckerContext &C) const { ProgramStateRef State = C.getState(); diff --git a/clang/test/Analysis/stream-error.c b/clang/test/Analysis/stream-error.c index 3c00e59bb6bd1..5ebdc32bb1b92 100644 --- a/clang/test/Analysis/stream-error.c +++ b/clang/test/Analysis/stream-error.c @@ -101,6 +101,24 @@ void error_fwrite(void) { Ret = fwrite(0, 1, 10, F); // expected-warning {{Stream might be already closed}} } +void error_fputc(void) { + FILE *F = tmpfile(); + if (!F) + return; + int Ret = fputc('X', F); + if (Ret == EOF) { + clang_analyzer_eval(ferror(F)); // expected-warning {{TRUE}} + clang_analyzer_eval(feof(F)); // expected-warning {{FALSE}} + fputc('Y', F); // expected-warning {{might be 'indeterminate'}} + } else { + clang_analyzer_eval(Ret == 'X'); // expected-warning {{TRUE}} + clang_analyzer_eval(feof(F) || ferror(F)); // expected-warning {{FALSE}} + fputc('Y', F); // no-warning + } + fclose(F); + fputc('A', F); // expected-warning {{Stream might be already closed}} +} + void freadwrite_zerosize(FILE *F) { size_t Ret; Ret = fwrite(0, 1, 0, F); @@ -241,6 +259,23 @@ void error_indeterminate_clearerr(void) { fclose(F); } +void error_indeterminate_fputc(void) { + FILE *F = fopen("file", "r+"); + if (!F) + return; + int rc = fseek(F, 0, SEEK_SET); + if (rc) { + if (feof(F)) { + fputc('X', F); // no warning + } else if (ferror(F)) { + fputc('C', F); // expected-warning {{might be 'indeterminate'}} + } else { + fputc('E', F); // expected-warning {{might be 'indeterminate'}} + } + } + fclose(F); +} + void error_indeterminate_feof1(void) { FILE *F = fopen("file", "r+"); if (!F) diff --git a/clang/test/Analysis/stream.c b/clang/test/Analysis/stream.c index a01310cfef5dd..9b6a31738d6e0 100644 --- a/clang/test/Analysis/stream.c +++ b/clang/test/Analysis/stream.c @@ -14,6 +14,12 @@ void check_fwrite(void) { fclose(fp); } +void check_fputc(void) { + FILE *fp = tmpfile(); + fputc('A', fp); // expected-warning {{Stream pointer might be NULL}} + fclose(fp); +} + void check_fseek(void) { FILE *fp = tmpfile(); fseek(fp, 0, 0); // expected-warning {{Stream pointer might be NULL}}