[llvm-branch-commits] [clang] a8d4cf1 - [clang] add APValue type check in `TryPrintAsStringLiteral`

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Aug 11 00:09:49 PDT 2022


Author: YingChi Long
Date: 2022-08-11T09:08:43+02:00
New Revision: a8d4cf1a0775a7dc1cefc694d9777cb3c7b7bd5f

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

LOG: [clang] add APValue type check in `TryPrintAsStringLiteral`

Fixes https://github.com/llvm/llvm-project/issues/57013

https://reviews.llvm.org/D115031 improved printing of non-type template
parameter args. But checking if the end of Inits is 0 without checking
if APValue is an integer, causes clang to segfault. This patch adds
the code to check the type. (May not be a proper bugfix.)

Reviewed By: aaron.ballman, lichray

Differential Revision: https://reviews.llvm.org/D131466

(cherry picked from commit 55d3b79d159bab53b140860279486db8a1c0e4f3)

Added: 
    clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp

Modified: 
    clang/lib/AST/APValue.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index a22031142c7c..d522c87388a5 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -637,10 +637,10 @@ static bool TryPrintAsStringLiteral(raw_ostream &Out,
     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 @@ static bool TryPrintAsStringLiteral(raw_ostream &Out,
   }
 
   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.

diff  --git a/clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp b/clang/test/SemaCXX/try-print-as-string-literal-type-check.cpp
new file mode 100644
index 000000000000..6d1b29e785c3
--- /dev/null
+++ b/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());


        


More information about the llvm-branch-commits mailing list