Author: YingChi Long Date: 2022-08-10T08:42:59+08:00 New Revision: 55d3b79d159bab53b140860279486db8a1c0e4f3
URL: https://github.com/llvm/llvm-project/commit/55d3b79d159bab53b140860279486db8a1c0e4f3 DIFF: https://github.com/llvm/llvm-project/commit/55d3b79d159bab53b140860279486db8a1c0e4f3.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 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 05886dc86d17d..634423008c2ed 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 0000000000000..6d1b29e785c31 --- /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()); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits