[libc-commits] [PATCH] D157534: [libc] Fix printf %a padding issue

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Aug 9 11:42:35 PDT 2023


michaelrj created this revision.
michaelrj added reviewers: sivachandra, lntue.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
michaelrj requested review of this revision.

The trailing zeroes were previously not counted when calculating the
padding, which caused a high-precision number to get too much padding.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157534

Files:
  libc/src/stdio/printf_core/float_hex_converter.h
  libc/test/src/stdio/sprintf_test.cpp


Index: libc/test/src/stdio/sprintf_test.cpp
===================================================================
--- libc/test/src/stdio/sprintf_test.cpp
+++ libc/test/src/stdio/sprintf_test.cpp
@@ -865,6 +865,19 @@
 
   written = __llvm_libc::sprintf(buff, "%+-#12.3a % 012.3a", 0.1256, 1256.0);
   ASSERT_STREQ_LEN(written, buff, "+0x1.014p-3   0x1.3a0p+10");
+
+  // Found via fuzzing
+
+  // The trailing zeroes were previously not counted when calculating the
+  // padding, which caused a high-precision number to get too much padding.
+  written = __llvm_libc::sprintf(buff, "%50.50a", 0x1.0p0);
+  ASSERT_STREQ_LEN(written, buff,
+                   "0x1.00000000000000000000000000000000000000000000000000p+0");
+
+  written = __llvm_libc::sprintf(buff, "%58.50a", 0x1.0p0);
+  ASSERT_STREQ_LEN(
+      written, buff,
+      " 0x1.00000000000000000000000000000000000000000000000000p+0");
 }
 
 TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
Index: libc/src/stdio/printf_core/float_hex_converter.h
===================================================================
--- libc/src/stdio/printf_core/float_hex_converter.h
+++ libc/src/stdio/printf_core/float_hex_converter.h
@@ -204,7 +204,7 @@
   constexpr int EXP_SEPERATOR_LEN = 1;
 
   padding = static_cast<int>(to_conv.min_width - (sign_char > 0 ? 1 : 0) -
-                             PREFIX_LEN - mant_digits -
+                             PREFIX_LEN - mant_digits - trailing_zeroes -
                              static_cast<int>(has_hexadecimal_point) -
                              EXP_SEPERATOR_LEN - (EXP_LEN - exp_cur));
   if (padding < 0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157534.548713.patch
Type: text/x-patch
Size: 1613 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230809/93501739/attachment.bin>


More information about the libc-commits mailing list