[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 15:30:46 PDT 2023


michaelrj updated this revision to Diff 548790.
michaelrj added a comment.

rewrite comment to clarify the bug being fixed


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157534/new/

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,20 @@
 
   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
+
+  // These tests check that the padding is properly calculated based on the
+  // min_width field. Specifically, they check that the extra zeroes added by
+  // the high precision are accounted for correctly.
+  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.548790.patch
Type: text/x-patch
Size: 1671 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230809/790825bc/attachment.bin>


More information about the libc-commits mailing list