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

Aleksey Shlyapnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 20 11:08:56 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL330458: [Sanitizer] Internal Printf string precision argument + padding. (authored by alekseyshl, committed by ).

Repository:
  rL LLVM

https://reviews.llvm.org/D45844

Files:
  compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc
  compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_printf_test.cc


Index: compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc
===================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc
+++ compiler-rt/trunk/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;
 }
 
Index: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_printf_test.cc
===================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_printf_test.cc
+++ compiler-rt/trunk/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


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


More information about the llvm-commits mailing list