[clang] [Clang][Sema] Add fortify warnings for fread, fwrite, and fgets (PR #204337)

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 22 11:02:08 PDT 2026


Radovan =?utf-8?q?Božić?= <radovan.bozic at htecgroup.com>,
Radovan =?utf-8?q?Božić?= <radovan.bozic at htecgroup.com>,
Radovan =?utf-8?q?Božić?= <radovan.bozic at htecgroup.com>,
Radovan =?utf-8?q?Božić?= <radovan.bozic at htecgroup.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/204337 at github.com>


================
@@ -1496,7 +1522,24 @@ void Sema::checkFortifiedBuiltinMemoryFunction(FunctionDecl *FD,
     Checker.checkSourceOverread(/*SrcArgIdx=*/0, /*SizeArgIdx=*/2);
     break;
   }
-
+  case Builtin::BIfread: {
+    DiagID = diag::warn_fortify_source_overflow;
+    SourceSize = Checker.ComputeExplicitObjectSizeArgumentProduct(1, 2);
+    DestinationSize = Checker.ComputeSizeArgument(0);
+    break;
+  }
+  case Builtin::BIfwrite: {
+    DiagID = diag::warn_fortify_source_overread;
+    SourceSize = Checker.ComputeExplicitObjectSizeArgumentProduct(1, 2);
+    DestinationSize = Checker.ComputeSizeArgument(0);
----------------
nickdesaulniers wrote:

No, please just rename them.  Feel free to do so in another PR (then rebase this PR on that) if you'd like. Or do so here.

```diff
    -  std::optional<llvm::APSInt> SourceSize;
    -  std::optional<llvm::APSInt> DestinationSize;
    +  std::optional<llvm::APSInt> AccessSize;
    +  std::optional<llvm::APSInt> BufferSize;
```

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


More information about the cfe-commits mailing list