[PATCH] D27800: [clang] Fix crash for sizeof on VLAs

Paulo Matos via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 12 07:08:38 PST 2022


pmatos updated this revision to Diff 399320.
pmatos retitled this revision from "Add overload of TransformToPotentiallyEvaluated for TypeSourceInfo" to "[clang] Fix crash for sizeof on VLAs".
pmatos edited the summary of this revision.
pmatos added a comment.

Ensure that we only call transform on Unevaluated Contexts, avoids the failure of a couple of vla tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D27800/new/

https://reviews.llvm.org/D27800

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/pr31042.cpp


Index: clang/test/SemaCXX/pr31042.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/pr31042.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -o - -emit-llvm -triple x86_64-unknown-linux-gnu -disable-free %s
+// We need to use -emit-llvm in order to trigger the error, without it semantic analysis
+// does not verify the used bit and there's no error.
+
+char a[1];
+
+void f1(void) {
+  int i = 0;
+  int j = sizeof(typeof(*(char(*)[i])a));
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -4499,6 +4499,10 @@
   }
 
   // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
+  if (isUnevaluatedContext() && ExprKind == UETT_SizeOf &&
+      TInfo->getType()->isVariablyModifiedType())
+    TInfo = TransformToPotentiallyEvaluated(TInfo);
+
   return new (Context) UnaryExprOrTypeTraitExpr(
       ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd());
 }
@@ -16601,6 +16605,16 @@
   return TransformToPE(*this).TransformExpr(E);
 }
 
+TypeSourceInfo *Sema::TransformToPotentiallyEvaluated(TypeSourceInfo *TInfo) {
+  assert(isUnevaluatedContext() &&
+         "Should only transform unevaluated expressions");
+  ExprEvalContexts.back().Context =
+      ExprEvalContexts[ExprEvalContexts.size() - 2].Context;
+  if (isUnevaluatedContext())
+    return TInfo;
+  return TransformToPE(*this).TransformType(TInfo);
+}
+
 void
 Sema::PushExpressionEvaluationContext(
     ExpressionEvaluationContext NewContext, Decl *LambdaContextDecl,
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -5053,6 +5053,7 @@
   void DiscardCleanupsInEvaluationContext();
 
   ExprResult TransformToPotentiallyEvaluated(Expr *E);
+  TypeSourceInfo *TransformToPotentiallyEvaluated(TypeSourceInfo *TInfo);
   ExprResult HandleExprEvaluationContextForTypeof(Expr *E);
 
   ExprResult CheckUnevaluatedOperand(Expr *E);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27800.399320.patch
Type: text/x-patch
Size: 2116 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220112/773caf6e/attachment.bin>


More information about the cfe-commits mailing list