[clang] [clang][analyzer] Add function 'fscanf' to StreamChecker. (PR #78180)

Ben Shi via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 22:09:27 PST 2024


================
@@ -975,6 +981,61 @@ void StreamChecker::evalFprintf(const FnDescription *Desc,
   C.addTransition(StateFailed);
 }
 
+void StreamChecker::evalFscanf(const FnDescription *Desc, const CallEvent &Call,
+                               CheckerContext &C) const {
+  ProgramStateRef State = C.getState();
+  if (Call.getNumArgs() < 2)
+    return;
+  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);
+
+  SValBuilder &SVB = C.getSValBuilder();
+  ASTContext &ACtx = C.getASTContext();
+
+  if (OldSS->ErrorState != ErrorFEof) {
----------------
benshi001 wrote:

Shall we add a comment like

```
'fscanf' return matched input items, this can be from zero to the total number of input items. 
If the stream read is successful but match fails, we still think 'fscanf' is success.
```

Something like that. You can reorganize my words.

https://github.com/llvm/llvm-project/pull/78180


More information about the cfe-commits mailing list