[clang] [clang][analyzer] Support `fputc` in StreamChecker (PR #71518)
Ben Shi via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 14 22:35:21 PST 2023
https://github.com/benshi001 updated https://github.com/llvm/llvm-project/pull/71518
>From 15367bae9f129b20885f13e5ca5ae816271d7214 Mon Sep 17 00:00:00 2001
From: Ben Shi <bennshi at tencent.com>
Date: Tue, 7 Nov 2023 16:44:05 +0800
Subject: [PATCH] [clang][analyzer] Support `fputc` in StreamChecker
---
.../StaticAnalyzer/Checkers/StreamChecker.cpp | 61 ++++++++++++++++---
clang/test/Analysis/stream-error.c | 35 +++++++++++
clang/test/Analysis/stream.c | 6 ++
3 files changed, 94 insertions(+), 8 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
index 69610029beb0a9e..df0865e85eaefda 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
@@ -245,11 +245,14 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
{{{"fclose"}, 1},
{&StreamChecker::preDefault, &StreamChecker::evalFclose, 0}},
{{{"fread"}, 4},
- {std::bind(&StreamChecker::preFreadFwrite, _1, _2, _3, _4, true),
+ {std::bind(&StreamChecker::preReadWrite, _1, _2, _3, _4, true),
std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, true), 3}},
{{{"fwrite"}, 4},
- {std::bind(&StreamChecker::preFreadFwrite, _1, _2, _3, _4, false),
+ {std::bind(&StreamChecker::preReadWrite, _1, _2, _3, _4, false),
std::bind(&StreamChecker::evalFreadFwrite, _1, _2, _3, _4, false), 3}},
+ {{{"fputc"}, 2},
+ {std::bind(&StreamChecker::preReadWrite, _1, _2, _3, _4, false),
+ &StreamChecker::evalFputc, 1}},
{{{"fseek"}, 3},
{&StreamChecker::preFseek, &StreamChecker::evalFseek, 0}},
{{{"ftell"}, 1},
@@ -305,12 +308,15 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
void evalFclose(const FnDescription *Desc, const CallEvent &Call,
CheckerContext &C) const;
- void preFreadFwrite(const FnDescription *Desc, const CallEvent &Call,
- CheckerContext &C, bool IsFread) const;
+ void preReadWrite(const FnDescription *Desc, const CallEvent &Call,
+ CheckerContext &C, bool IsRead) const;
void evalFreadFwrite(const FnDescription *Desc, const CallEvent &Call,
CheckerContext &C, bool IsFread) const;
+ void evalFputc(const FnDescription *Desc, const CallEvent &Call,
+ CheckerContext &C) const;
+
void preFseek(const FnDescription *Desc, const CallEvent &Call,
CheckerContext &C) const;
void evalFseek(const FnDescription *Desc, const CallEvent &Call,
@@ -634,9 +640,9 @@ void StreamChecker::evalFclose(const FnDescription *Desc, const CallEvent &Call,
C.addTransition(StateFailure);
}
-void StreamChecker::preFreadFwrite(const FnDescription *Desc,
- const CallEvent &Call, CheckerContext &C,
- bool IsFread) const {
+void StreamChecker::preReadWrite(const FnDescription *Desc,
+ const CallEvent &Call, CheckerContext &C,
+ bool IsRead) const {
ProgramStateRef State = C.getState();
SVal StreamVal = getStreamArg(Desc, Call);
State = ensureStreamNonNull(StreamVal, Call.getArgExpr(Desc->StreamArgNo), 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<CallExpr>(Call.getOriginExpr());
+ if (!CE)
+ return;
+
+ const StreamState *OldSS = State->get<StreamMap>(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<NonLoc> PutVal = Call.getArgSVal(0).getAs<NonLoc>();
+ if (!PutVal)
+ return;
+ ProgramStateRef StateNotFailed =
+ State->BindExpr(CE, C.getLocationContext(), *PutVal);
+ StateNotFailed =
+ StateNotFailed->set<StreamMap>(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<StreamMap>(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 3c00e59bb6bd19d..5ebdc32bb1b92ff 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 a01310cfef5dd8a..9b6a31738d6e0b8 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}}
More information about the cfe-commits
mailing list