Skip to content

Commit

Permalink
lib/test_printf.c: test precision quirks
Browse files Browse the repository at this point in the history
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 <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Joe Perches <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Maurizio Lombardi <[email protected]>
Cc: Tejun Heo <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Villemoes authored and torvalds committed Jan 16, 2016
1 parent 331e4de commit f176eb4
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/test_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,23 @@ test_string(void)
test("", "%s%.0s", "", "123");
test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
test("1234 ", "%-10.4s", "123456");
test(" 1234", "%10.4s", "123456");
/*
* POSIX and C99 say that a missing precision should be
* treated as a precision of 0. However, the kernel's printf
* implementation treats this case as if the . wasn't
* present. Let's add a test case documenting the current
* behaviour; should anyone ever feel the need to follow the
* standards more closely, this can be revisited.
* POSIX and C99 say that a negative precision (which is only
* possible to pass via a * argument) should be treated as if
* the precision wasn't present, and that if the precision is
* omitted (as in %.s), the precision should be taken to be
* 0. However, the kernel's printf behave exactly opposite,
* treating a negative precision as 0 and treating an omitted
* precision specifier as if no precision was given.
*
* These test cases document the current behaviour; should
* anyone ever feel the need to follow the standards more
* closely, this can be revisited.
*/
test(" ", "%4.*s", -5, "123456");
test("123456", "%.s", "123456");
test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
}
Expand Down

0 comments on commit f176eb4

Please sign in to comment.