[clang] Avoid printing overly large integer. (PR #75902)
Nhat Nguyen via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 11 23:10:41 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>,
=?utf-8?q?“Nhat?= <nhat7203 at gmail.com>,
=?utf-8?q?“Nhat?= <nhat7203 at gmail.com>,Nhat Nguyen
<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/7] 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/7] 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/7] 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/7] 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;
}
>From 35773add17d8b4e884daac0e3e7dc312f1947911 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <nhat7203 at gmail.com>
Date: Mon, 1 Jan 2024 21:09:21 -0500
Subject: [PATCH 5/7] move the condition right under toString
---
clang/lib/Sema/SemaDeclCXX.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 80e6cd9ee07420..d3b68c9aa95645 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17132,10 +17132,6 @@ 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()) {
- return false;
- }
uint32_t CodeUnit = static_cast<uint32_t>(V.getInt().getZExtValue());
WriteCharTypePrefix(BTy->getKind(), OS);
OS << '\'';
@@ -17151,6 +17147,10 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
}
}
V.getInt().toString(Str);
+ if (V.getInt() > std::numeric_limits<uint64_t>::max() ||
+ V.getInt() < std::numeric_limits<int64_t>::min()) {
+ return false;
+ }
}
break;
>From abbbe6a54d08a25d6b6707bdaf8795640a3cabdf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=80=9CNhat?= <nhat7203 at gmail.com>
Date: Sat, 10 Feb 2024 13:31:22 -0500
Subject: [PATCH 6/7] print hexa
---
clang/lib/Sema/SemaDeclCXX.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index d3b68c9aa95645..80c1888fde6843 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17146,10 +17146,12 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
break;
}
}
- V.getInt().toString(Str);
if (V.getInt() > std::numeric_limits<uint64_t>::max() ||
V.getInt() < std::numeric_limits<int64_t>::min()) {
return false;
+ V.getInt().toString(Str, 16);
+ } else {
+ V.getInt().toString(Str);
}
}
>From c5865b4a2665ef4b6bfd21d812a00a7649c635eb Mon Sep 17 00:00:00 2001
From: Nhat Nguyen <nhat7203 at gmail.com>
Date: Mon, 12 Feb 2024 02:10:31 -0500
Subject: [PATCH 7/7] Update clang/lib/Sema/SemaDeclCXX.cpp
Co-authored-by: Yueh-Shun Li <shamrocklee at posteo.net>
---
clang/lib/Sema/SemaDeclCXX.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 80c1888fde6843..4a5a8712d49e79 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17148,7 +17148,6 @@ static bool ConvertAPValueToString(const APValue &V, QualType T,
}
if (V.getInt() > std::numeric_limits<uint64_t>::max() ||
V.getInt() < std::numeric_limits<int64_t>::min()) {
- return false;
V.getInt().toString(Str, 16);
} else {
V.getInt().toString(Str);
More information about the cfe-commits
mailing list