Skip to content

[ADT][bugfix] Fixed extra leading zero in uhextostr #141097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dybv-sc
Copy link
Contributor

@dybv-sc dybv-sc commented May 22, 2025

fixed bug: if fixed-width mode uhextostr() is used with value zero, it prints extra '0' character.

fixed bug: if fixed-width mode uhextostr() is used with value zero, it prints
extra '0' character.
@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-llvm-adt

Author: Bushev Dmitry (dybv-sc)

Changes

fixed bug: if fixed-width mode uhextostr() is used with value zero, it prints extra '0' character.


Full diff: https://github.com/llvm/llvm-project/pull/141097.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/StringExtras.h (+2-1)
  • (modified) llvm/unittests/ADT/StringExtrasTest.cpp (+1)
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index a2cc36dd8caad..7d81c63485be2 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -179,7 +179,8 @@ inline std::string utohexstr(uint64_t X, bool LowerCase = false,
   char Buffer[17];
   char *BufPtr = std::end(Buffer);
 
-  if (X == 0) *--BufPtr = '0';
+  if (X == 0 && !Width)
+    *--BufPtr = '0';
 
   for (unsigned i = 0; Width ? (i < Width) : X; ++i) {
     unsigned char Mod = static_cast<unsigned char>(X) & 15;
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 36908a98aa2ae..fbaed38da5943 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -144,6 +144,7 @@ TEST(StringExtrasTest, ToAndFromHex) {
 }
 
 TEST(StringExtrasTest, UINT64ToHex) {
+  EXPECT_EQ(utohexstr(0x0u, false, 2), "00");
   EXPECT_EQ(utohexstr(0xA0u), "A0");
   EXPECT_EQ(utohexstr(0xA0u, false, 4), "00A0");
   EXPECT_EQ(utohexstr(0xA0u, false, 8), "000000A0");

@dybv-sc dybv-sc requested review from dwblaikie and kuhar May 22, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants