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

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 16 15:00:57 PDT 2026


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>


================
@@ -1174,18 +1174,28 @@ class FortifiedBufferChecker {
     return NewIndex;
   }
 
-  std::optional<llvm::APSInt>
-  ComputeExplicitObjectSizeArgument(unsigned Index) {
+  /// Evaluate the argument at Index as an integer constant while preserving
+  /// its signedness, or return std::nullopt if it cannot be evaluated.
+  std::optional<llvm::APSInt> EvaluateIntegerArgument(unsigned Index) {
     std::optional<unsigned> IndexOptional = TranslateIndex(Index);
     if (!IndexOptional)
       return std::nullopt;
-    unsigned NewIndex = *IndexOptional;
+
     Expr::EvalResult Result;
-    Expr *SizeArg = TheCall->getArg(NewIndex);
-    if (!SizeArg->EvaluateAsInt(Result, S.getASTContext()))
+    Expr *Arg = TheCall->getArg(*IndexOptional);
----------------
nickdesaulniers wrote:

This looks like it's missing a bounds check against `TheCall->getNumArgs()` to me.

If we add one here, can we remove the one in `ComputeSizeArgument`?

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


More information about the cfe-commits mailing list