[clang] Avoid printing overly large integer. (PR #75902)

Nhat Nguyen via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 1 17:59:50 PST 2024


=?utf-8?q?“Nhat?= <nhat7203 at gmail.com>,
=?utf-8?q?“Nhat?= <nhat7203 at gmail.com>,
=?utf-8?q?“Nhat?= <nhat7203 at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/75902 at github.com>


https://github.com/changkhothuychung updated https://github.com/llvm/llvm-project/pull/75902

>From 4939edb1cb2b73f9c60c4cce0803fab4888beb6e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <nhat7203 at gmail.com>
Date: Mon, 1 Jan 2024 20:59:26 -0500
Subject: [PATCH 1/4] return false if the value is too large

---
 clang/lib/Sema/SemaDeclCXX.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index a3f68d4ffc0f6e..18631ec7dac152 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17132,6 +17132,9 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
         case BuiltinType::WChar_U: {
           unsigned TyWidth = Context.getIntWidth(T);
           assert(8 <= TyWidth && TyWidth <= 32 && "Unexpected integer width");
+          if (V.getInt() >= (1 << 64)) {
+            return false;
+          }
           uint32_t CodeUnit = static_cast<uint32_t>(V.getInt().getZExtValue());
           WriteCharTypePrefix(BTy->getKind(), OS);
           OS << '\'';

>From 1932765f174e187a79144c4fd69657dc7bae480a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <nhat7203 at gmail.com>
Date: Mon, 1 Jan 2024 20:59:27 -0500
Subject: [PATCH 2/4] Update clang/lib/Sema/SemaDeclCXX.cpp

Co-authored-by: Yueh-Shun Li <shamrocklee at posteo.net>
---
 clang/lib/Sema/SemaDeclCXX.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 18631ec7dac152..196e32a4a349f2 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17132,7 +17132,7 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
         case BuiltinType::WChar_U: {
           unsigned TyWidth = Context.getIntWidth(T);
           assert(8 <= TyWidth && TyWidth <= 32 && "Unexpected integer width");
-          if (V.getInt() >= (1 << 64)) {
+          if (V.getInt() > std::numeric_limits<uint64_t>::max() || V.getInt() < std::numeric_limits<int64_t>::min()) {
             return false;
           }
           uint32_t CodeUnit = static_cast<uint32_t>(V.getInt().getZExtValue());

>From 69e21b2bf76b51914f835c74af9e2b3c685ffae0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <nhat7203 at gmail.com>
Date: Mon, 1 Jan 2024 20:59:28 -0500
Subject: [PATCH 3/4] clang format

---
 clang/lib/Sema/SemaDeclCXX.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 196e32a4a349f2..323890b38daeec 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17132,7 +17132,8 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
         case BuiltinType::WChar_U: {
           unsigned TyWidth = Context.getIntWidth(T);
           assert(8 <= TyWidth && TyWidth <= 32 && "Unexpected integer width");
-          if (V.getInt() > std::numeric_limits<uint64_t>::max() || V.getInt() < std::numeric_limits<int64_t>::min()) {
+          if (V.getInt() > std::numeric_limits<uint64_t>::max() || 
+              V.getInt() < std::numeric_limits<int64_t>::min()) {
             return false;
           }
           uint32_t CodeUnit = static_cast<uint32_t>(V.getInt().getZExtValue());

>From ac93beb74cbe6e0cb6383ece6b72b59b00aeca0d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <nhat7203 at gmail.com>
Date: Mon, 1 Jan 2024 20:59:29 -0500
Subject: [PATCH 4/4] clang format

---
 clang/lib/Sema/SemaDeclCXX.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 323890b38daeec..80e6cd9ee07420 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17132,7 +17132,7 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
         case BuiltinType::WChar_U: {
           unsigned TyWidth = Context.getIntWidth(T);
           assert(8 <= TyWidth && TyWidth <= 32 && "Unexpected integer width");
-          if (V.getInt() > std::numeric_limits<uint64_t>::max() || 
+          if (V.getInt() > std::numeric_limits<uint64_t>::max() ||
               V.getInt() < std::numeric_limits<int64_t>::min()) {
             return false;
           }



More information about the cfe-commits mailing list