[PATCH] D45844: [Sanitizer] Internal Printf string precision argument + padding.

Aleksey Shlyapnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 19 15:34:15 PDT 2018


alekseyshl created this revision.
alekseyshl added a reviewer: eugenis.
Herald added subscribers: Sanitizers, delcypher, kubamracek.

Example:

  Printf("%.*s", 5, "123");

should yield:

  '123  '

In case Printf's requested string precision is larger than the string
argument, the resulting string should be padded up to the requested
precision.

For the simplicity sake, implementing right padding only.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D45844

Files:
  lib/sanitizer_common/sanitizer_printf.cc
  lib/sanitizer_common/tests/sanitizer_printf_test.cc


Index: lib/sanitizer_common/tests/sanitizer_printf_test.cc
===================================================================
--- lib/sanitizer_common/tests/sanitizer_printf_test.cc
+++ lib/sanitizer_common/tests/sanitizer_printf_test.cc
@@ -146,8 +146,13 @@
   EXPECT_EQ(3U, len);
   EXPECT_STREQ("123", buf);
   len = internal_snprintf(buf, sizeof(buf), "%.*s", 6, "12345");
-  EXPECT_EQ(5U, len);
-  EXPECT_STREQ("12345", buf);
+  EXPECT_EQ(6U, len);
+  EXPECT_STREQ("12345 ", buf);
+  // CHeck that precision does not overflow the smaller buffer, although
+  // 10 chars is requested, it stops at the buffer size, 8.
+  len = internal_snprintf(buf, 8, "%.*s", 10, "12345");
+  EXPECT_EQ(10U, len);  // The required size reported.
+  EXPECT_STREQ("12345  ", buf);
 }
 
 }  // namespace __sanitizer
Index: lib/sanitizer_common/sanitizer_printf.cc
===================================================================
--- lib/sanitizer_common/sanitizer_printf.cc
+++ lib/sanitizer_common/sanitizer_printf.cc
@@ -105,6 +105,8 @@
       break;
     result += AppendChar(buff, buff_end, *s);
   }
+  while (result < precision)
+    result += AppendChar(buff, buff_end, ' ');
   return result;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45844.143177.patch
Type: text/x-patch
Size: 1195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180419/f83433ac/attachment.bin>


More information about the llvm-commits mailing list