[clang] [analyzer] Fix crashes on unusual macros in tryExpandAsInteger (PR #248108)
Endre Fülöp via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 3 02:44:58 PST 2026
https://github.com/gamesh411 updated https://github.com/llvm/llvm-project/pull/248108
>From 13a6de6deff9c9a311e8295e305b3ac491adfe4a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?= <endre.fulop at sigmatechnology.com>
Date: Mon, 23 Nov 2026 12:06:30 +0100
Subject: [PATCH 1/3] [analyzer] Fix crashes on unusual macros in
tryExpandAsInteger
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
---
clang/docs/ReleaseNotes.md | 4 +++
.../Core/PathSensitive/CheckerHelpers.h | 5 ++--
.../StaticAnalyzer/Core/CheckerHelpers.cpp | 14 +++++++++-
.../std-c-library-functions-macro-range.c | 26 +++++++++++++++++++
4 files changed, 46 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Analysis/std-c-library-functions-macro-range.c
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index c221b0d7f0fa08..d545fbab250c7f 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1727,6 +1727,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)
+
% comment:
% This is for the Static Analyzer.
% Use `####` headings for subsections:
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
index 4546cb718cdc81..66f3e0fe888bd9 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..42c432c8a2827a
--- /dev/null
+++ b/clang/test/Analysis/std-c-library-functions-macro-range.c
@@ -0,0 +1,26 @@
+// 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=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}}
+ }
+}
>From c14b4938567d8fd3453a95d787a69a856c7b076f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?= <endre.fulop at sigmatechnology.com>
Date: Wed, 2 Dec 2026 20:24:24 +0100
Subject: [PATCH 2/3] start the last sentence on a new line
---
.../clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
index 66f3e0fe888bd9..00722bbd14b148 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h
@@ -70,8 +70,8 @@ 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, 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.
+/// 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 {
>From e6ea530fe81ffcd0bb56c0d1969305cd9a7b1cdb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Endre=20F=C3=BCl=C3=B6p?= <endre.fulop at sigmatechnology.com>
Date: Wed, 2 Dec 2026 20:24:25 +0100
Subject: [PATCH 3/3] test the plain empty EOF too
---
clang/test/Analysis/std-c-library-functions-macro-range.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/test/Analysis/std-c-library-functions-macro-range.c b/clang/test/Analysis/std-c-library-functions-macro-range.c
index 42c432c8a2827a..c4c5c38cddba0a 100644
--- a/clang/test/Analysis/std-c-library-functions-macro-range.c
+++ b/clang/test/Analysis/std-c-library-functions-macro-range.c
@@ -3,6 +3,7 @@
// 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
More information about the cfe-commits
mailing list