[clang] d2ca52f - [analyzer] Fix crashes on unusual macros in tryExpandAsInteger (#248108)

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 3 03:31:11 PST 2026


Author: Endre Fülöp
Date: 2026-12-03T11:31:04Z
New Revision: d2ca52f2c36b8db5362585fb02a3f1ce01027e69

URL: https://github.com/llvm/llvm-project/commit/d2ca52f2c36b8db5362585fb02a3f1ce01027e69
DIFF: https://github.com/llvm/llvm-project/commit/d2ca52f2c36b8db5362585fb02a3f1ce01027e69.diff

LOG: [analyzer] Fix crashes on unusual macros in tryExpandAsInteger (#248108)

tryExpandAsInteger crashed on empty macro definitions because of an
unchecked .back() call, and an assertion fired when the value of a macro
did not fit into int64_t. Values wider than an int that still fit into
int64_t were silently truncated. Return std::nullopt in these cases.

Values above INT_MAX that fit into the bits of the target's int are
still converted to it, because DragonFly BSD defines AT_FDCWD as
0xFFFAFDCD.

Fixes #246164.

Assisted-by: Claude Code

Added: 
    clang/test/Analysis/std-c-library-functions-macro-range.c

Modified: 
    clang/docs/ReleaseNotes.md
    clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
    clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a6c123abb4bbc2..e87f00c687ab6b 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1743,6 +1743,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
   (such as `read`) via fuzzy builtin matching, and added explicit
   `CStringChecker` modeling for C23 `memset_explicit`.
 
+- Fixed crashes when a macro whose value the analyzer reads, such as `EOF` or
+  `O_NONBLOCK`, is defined as empty or as an integer that does not fit into
+  `int64_t`. (#GH246164)
+  
 - Fixed a regression in the `alpha.unix.PthreadLock` checker, introduced in
   Clang 11, where a zero return value of the XNU try-lock functions
   (`lck_mtx_try_lock`, `lck_rw_try_lock_exclusive` and

diff  --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
index 4546cb718cdc81..00722bbd14b148 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
@@ -69,8 +69,9 @@ Nullability getNullabilityAnnotation(QualType Type);
 
 /// Try to parse the value of a defined preprocessor macro. We can only parse
 /// simple expressions that consist of an optional minus sign token and then a
-/// token for an integer. If we cannot parse the value then std::nullopt is
-/// returned.
+/// token for an integer. If we cannot parse the value, or it is outside the
+/// target's [INT_MIN, UINT_MAX], then std::nullopt is returned.
+/// A value above INT_MAX is converted to int, so 0xFFFFFFFF yields -1.
 std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP);
 
 class OperatorKind {

diff  --git a/clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp b/clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
index cb0824be69dd3b..07dbeeebc7719d 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerHelpers.cpp
@@ -13,7 +13,9 @@
 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/Expr.h"
+#include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Preprocessor.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
 #include "llvm/ADT/STLExtras.h"
 #include <iterator>
@@ -121,6 +123,9 @@ std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP) {
   };
   llvm::copy_if(MI->tokens(), std::back_inserter(FilteredTokens), IsNotParen);
 
+  if (FilteredTokens.empty())
+    return std::nullopt;
+
   // Parse an integer at the end of the macro definition.
   const Token &T = FilteredTokens.back();
 
@@ -154,7 +159,14 @@ std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP) {
     }
   }
 
-  return IntValue.getExtValue();
+  // System headers may spell a negative int as a literal above INT_MAX (e.g.
+  // 0xFFFFFFFF), so accept every value that fits into the bits of an int.
+  const APSIntType IntType(PP.getTargetInfo().getIntWidth(),
+                           /*Unsigned=*/false);
+  if (IntType.testInRange(IntValue, /*AllowMixedSign=*/true) !=
+      APSIntType::RTR_Within)
+    return std::nullopt;
+  return IntType.convert(IntValue).getExtValue();
 }
 
 OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK,

diff  --git a/clang/test/Analysis/std-c-library-functions-macro-range.c b/clang/test/Analysis/std-c-library-functions-macro-range.c
new file mode 100644
index 00000000000000..c4c5c38cddba0a
--- /dev/null
+++ b/clang/test/Analysis/std-c-library-functions-macro-range.c
@@ -0,0 +1,27 @@
+// DEFINE: %{analyze} = %clang_analyze_cc1 %s -triple x86_64-unknown-linux-gnu \
+// DEFINE:   -analyzer-checker=core,unix.StdCLibraryFunctions,debug.ExprInspection
+
+// An EOF that is empty or outside the [INT_MIN, UINT_MAX] of the target is
+// ignored, and the default of -1 is used. The int of AVR has 16 bits.
+// RUN: %{analyze} -verify=ignored '-DEOF='
+// RUN: %{analyze} -verify=ignored '-DEOF=()'
+// RUN: %{analyze} -verify=ignored -DEOF=0x100000000
+// RUN: %{analyze} -verify=ignored -DEOF=-0x80000001
+// RUN: %{analyze} -verify=ignored -DEOF=0x8000000000000000
+// RUN: %{analyze} -verify=ignored -DEOF=0x10000 -triple avr
+
+// A value above INT_MAX in that range is converted to int.
+// RUN: %{analyze} -verify=converted -DEOF=0xFFFFFFFE
+
+void clang_analyzer_eval(int);
+
+typedef struct FILE FILE;
+int getc(FILE *);
+
+void test_getc(FILE *fp) {
+  int y = getc(fp);
+  if (y < 0) {
+    clang_analyzer_eval(y == -1); // ignored-warning{{TRUE}} converted-warning{{FALSE}}
+    clang_analyzer_eval(y == -2); // ignored-warning{{FALSE}} converted-warning{{TRUE}}
+  }
+}


        


More information about the cfe-commits mailing list