[PATCH] D155423: [ADT] Fix `utohexstr` of zero with fixed width

Alexander Us via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 16 23:42:13 PDT 2023


IllogicalMagic created this revision.
IllogicalMagic added reviewers: ychen, dblaikie.
Herald added a project: All.
IllogicalMagic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Fixed-width implementation of `utohexstr` contained a bug where extra zero was added when width argument is present and converted value is zero.

Add check for zero width in zero-handling path to fix the issue. Add unit tests for zero input.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155423

Files:
  llvm/include/llvm/ADT/StringExtras.h
  llvm/unittests/ADT/StringExtrasTest.cpp


Index: llvm/unittests/ADT/StringExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/StringExtrasTest.cpp
+++ llvm/unittests/ADT/StringExtrasTest.cpp
@@ -100,6 +100,8 @@
   EXPECT_EQ(utohexstr(0xA0u), "A0");
   EXPECT_EQ(utohexstr(0xA0u, false, 4), "00A0");
   EXPECT_EQ(utohexstr(0xA0u, false, 8), "000000A0");
+  EXPECT_EQ(utohexstr(0x0u), "0");
+  EXPECT_EQ(utohexstr(0x0u, false, 4), "0000");
 }
 
 TEST(StringExtrasTest, to_float) {
Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -154,7 +154,8 @@
   char Buffer[17];
   char *BufPtr = std::end(Buffer);
 
-  if (X == 0) *--BufPtr = '0';
+  if (X == 0 && Width == 0)
+    *--BufPtr = '0';
 
   for (unsigned i = 0; Width ? (i < Width) : X; ++i) {
     unsigned char Mod = static_cast<unsigned char>(X) & 15;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155423.540874.patch
Type: text/x-patch
Size: 965 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230717/61113c01/attachment.bin>


More information about the llvm-commits mailing list