[clang] [clang][analyzer] Change modeling of 'fileno' in checkers. (PR #81842)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 16 06:29:07 PST 2024


================
@@ -1404,6 +1486,47 @@ void StreamChecker::evalFeofFerror(const FnDescription *Desc,
   }
 }
 
+void StreamChecker::evalFileno(const FnDescription *Desc, const CallEvent &Call,
+                               CheckerContext &C) const {
+  // Fileno should fail only if the passed pointer is invalid.
+  // Some of the preconditions are checked already in preDefault.
+  // Here we can assume that the operation does not fail.
+  // An added failure case causes many unexpected warnings because a file number
+  // becomes -1 that is not expected by the program.
+  // The stream error states are not modified by 'fileno', and not the 'errno'.
+  // (To ensure that errno is not changed, this evalCall is needed to not
+  // invalidate 'errno' like in a default case.)
----------------
NagyDonat wrote:

```suggestion
  // Here we assume that the operation does not fail, because we introduced a
  // separate branch where fileno() returns -1, then it would cause many
  // unexpected and unwanted warnings in situations where fileno() is called
  // on vaild streams.
  // The stream error states are not modified by 'fileno', and 'errno' is also
  // left unchanged (so this evalCall does not invalidate it).
```

I felt that this comment is a bit difficult to understand and composed a reworded alternative. Of course, this is a very subjective matter and English isn't my first language, so feel free to bikeshed this and/or override my suggestions.

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


More information about the cfe-commits mailing list