diff options
author | Rasmus Villemoes <linux@rasmusvillemoes.dk> | 2016-01-15 19:58:56 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2016-01-16 14:17:28 -0500 |
commit | f176eb4ce9d433184178d37a598ffdfe1cee1463 (patch) | |
tree | 40fe2de0113099e8989119591d13ac8339ded1ba /lib/test_printf.c | |
parent | 331e4deb6dfdac50d3f9c4ccbc41b1427335e212 (diff) |
lib/test_printf.c: test precision quirks
The kernel's printf doesn't follow the standards in a few corner cases
(which are probably mostly irrelevant). Add tests that document the
current behaviour.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Al Viro <viro@ZenIV.linux.org.uk>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Joe Perches <joe@perches.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Maurizio Lombardi <mlombard@redhat.com>
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/test_printf.c')
-rw-r--r-- | lib/test_printf.c | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/test_printf.c b/lib/test_printf.c index 1ce1a1dd8faf..3393d667c6b8 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c | |||
@@ -166,14 +166,23 @@ test_string(void) | |||
166 | test("", "%s%.0s", "", "123"); | 166 | test("", "%s%.0s", "", "123"); |
167 | test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456"); | 167 | test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456"); |
168 | test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5"); | 168 | test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5"); |
169 | test("1234 ", "%-10.4s", "123456"); | ||
170 | test(" 1234", "%10.4s", "123456"); | ||
169 | /* | 171 | /* |
170 | * POSIX and C99 say that a missing precision should be | 172 | * POSIX and C99 say that a negative precision (which is only |
171 | * treated as a precision of 0. However, the kernel's printf | 173 | * possible to pass via a * argument) should be treated as if |
172 | * implementation treats this case as if the . wasn't | 174 | * the precision wasn't present, and that if the precision is |
173 | * present. Let's add a test case documenting the current | 175 | * omitted (as in %.s), the precision should be taken to be |
174 | * behaviour; should anyone ever feel the need to follow the | 176 | * 0. However, the kernel's printf behave exactly opposite, |
175 | * standards more closely, this can be revisited. | 177 | * treating a negative precision as 0 and treating an omitted |
178 | * precision specifier as if no precision was given. | ||
179 | * | ||
180 | * These test cases document the current behaviour; should | ||
181 | * anyone ever feel the need to follow the standards more | ||
182 | * closely, this can be revisited. | ||
176 | */ | 183 | */ |
184 | test(" ", "%4.*s", -5, "123456"); | ||
185 | test("123456", "%.s", "123456"); | ||
177 | test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c"); | 186 | test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c"); |
178 | test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c"); | 187 | test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c"); |
179 | } | 188 | } |