[clang] [clang] Check null TypeSourceInfo in CreateUnaryExprOrTypeTraitExpr (PR #112111)

Andrew Sukach via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 15 09:27:22 PDT 2024


https://github.com/sookach updated https://github.com/llvm/llvm-project/pull/112111

>From 64bde2ed7a39d5e270a7d1daca3a30a2cde6e0db Mon Sep 17 00:00:00 2001
From: Andrew Sukach <andrewsukach at gmail.com>
Date: Sat, 12 Oct 2024 19:47:30 -0400
Subject: [PATCH] [clang] Check for null TypeSourceInfo in
 Sema::CreateUnaryExprOrTypeTraitExpr

---
 clang/docs/ReleaseNotes.rst                   |  2 ++
 clang/lib/Sema/SemaExpr.cpp                   |  3 +++
 .../unary-expr-or-type-trait-invalid.cpp      | 26 +++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100644 clang/test/SemaCXX/unary-expr-or-type-trait-invalid.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 763bc3ac159322..f2f6988a1f5c13 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -505,6 +505,8 @@ Bug Fixes to C++ Support
 - Fix a crash when parsing a pseudo destructor involving an invalid type. (#GH111460)
 - Fixed an assertion failure when invoking recovery call expressions with explicit attributes
   and undeclared templates. (#GH107047, #GH49093)
+- Fixed a compiler crash that occurred when processing malformed code involving `sizeof` with
+  an invalid type argument. (#GH111594)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 4e37385710af5e..b0bd216c5dc101 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -4629,6 +4629,9 @@ ExprResult Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo,
       TInfo->getType()->isVariablyModifiedType())
     TInfo = TransformToPotentiallyEvaluated(TInfo);
 
+  if (!TInfo)
+    return ExprError();
+
   // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
   return new (Context) UnaryExprOrTypeTraitExpr(
       ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd());
diff --git a/clang/test/SemaCXX/unary-expr-or-type-trait-invalid.cpp b/clang/test/SemaCXX/unary-expr-or-type-trait-invalid.cpp
new file mode 100644
index 00000000000000..b2b959be8d31ff
--- /dev/null
+++ b/clang/test/SemaCXX/unary-expr-or-type-trait-invalid.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused-value %s
+
+a() {struct b c (sizeof(b * [({ {tree->d* next)} 0
+
+// expected-error at 3 {{a type specifier is required for all declarations}}
+// expected-error at 3 {{use of undeclared identifier 'tree'; did you mean 'true'?}}
+// expected-error at 3 {{member reference type 'bool' is not a pointer}}
+// expected-error at 3 {{expected ';' after expression}}
+// expected-error at 3 {{use of undeclared identifier 'next'; did you mean 'new'?}}
+// expected-error at 3 {{expected expression}}
+// expected-error at 3 {{expected ';' after expression}}
+// expected-error at 26 {{expected '}'}}
+// expected-note at 3 {{to match this '{'}}
+// expected-error at 26 {{expected ')'}}
+// expected-note at 3 {{to match this '('}}
+// expected-error at 26 {{expected ']'}}
+// expected-note at 3 {{to match this '['}}
+// expected-error at 26 {{expected ')'}}
+// expected-note at 3 {{to match this '('}}
+// expected-error at 3 {{using declaration 'exp' instantiates to an empty pack}}
+// expected-error at 3 {{variable has incomplete type 'struct b'}}
+// expected-note at 3 {{forward declaration of 'b'}}
+// expected-error at 3 {{expected ';' at end of declaration}}
+// expected-error at 26 {{expected '}'}}
+// expected-note at 3 {{to match this '{'}}
+



More information about the cfe-commits mailing list