[compiler-rt] r330458 - [Sanitizer] Internal Printf string precision argument + padding.

Alex Shlyapnikov via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 20 11:03:10 PDT 2018


Author: alekseyshl
Date: Fri Apr 20 11:03:10 2018
New Revision: 330458

URL: http://llvm.org/viewvc/llvm-project?rev=330458&view=rev
Log:
[Sanitizer] Internal Printf string precision argument + padding.

Summary:
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.

Reviewers: eugenis

Subscribers: kubamracek, delcypher, #sanitizers, llvm-commits

Differential Revision: https://reviews.llvm.org/D45844

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

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc?rev=330458&r1=330457&r2=330458&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc Fri Apr 20 11:03:10 2018
@@ -105,6 +105,8 @@ static int AppendString(char **buff, con
       break;
     result += AppendChar(buff, buff_end, *s);
   }
+  while (result < precision)
+    result += AppendChar(buff, buff_end, ' ');
   return result;
 }
 

Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_printf_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_printf_test.cc?rev=330458&r1=330457&r2=330458&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_printf_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_printf_test.cc Fri Apr 20 11:03:10 2018
@@ -146,8 +146,13 @@ TEST(Printf, Precision) {
   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




More information about the llvm-commits mailing list