[PATCH] D131466: [clang] add APValue type check in `TryPrintAsStringLiteral`

YingChi Long via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 9 10:13:31 PDT 2022


inclyc updated this revision to Diff 451195.
inclyc added a comment.

address comments


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D131466

Files:
  clang/lib/AST/APValue.cpp
  clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp


Index: clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -verify
+// expected-no-diagnostics
+
+// Reported by: https://github.com/llvm/llvm-project/issues/57013
+// The following code should not crash clang
+struct X {
+  char arr[2];
+  constexpr X() {}
+  constexpr void modify() {
+    arr[0] = 0;
+  }
+};
+constexpr X f(X t) {
+    t.modify();
+    return t;
+}
+auto x = f(X());
Index: clang/lib/AST/APValue.cpp
===================================================================
--- clang/lib/AST/APValue.cpp
+++ clang/lib/AST/APValue.cpp
@@ -637,10 +637,10 @@
     return false;
 
   // Nothing we can do about a sequence that is not null-terminated
-  if (!Inits.back().getInt().isZero())
+  if (!Inits.back().isInt() || !Inits.back().getInt().isZero())
     return false;
-  else
-    Inits = Inits.drop_back();
+
+  Inits = Inits.drop_back();
 
   llvm::SmallString<40> Buf;
   Buf.push_back('"');
@@ -655,6 +655,8 @@
   }
 
   for (auto &Val : Inits) {
+    if (!Val.isInt())
+      return false;
     int64_t Char64 = Val.getInt().getExtValue();
     if (!isASCII(Char64))
       return false; // Bye bye, see you in integers.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131466.451195.patch
Type: text/x-patch
Size: 1366 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220809/9ea754bd/attachment.bin>


More information about the cfe-commits mailing list