From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754168AbbLCUvw (ORCPT ); Thu, 3 Dec 2015 15:51:52 -0500 Received: from mail-lb0-f182.google.com ([209.85.217.182]:36002 "EHLO mail-lb0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754133AbbLCUvu (ORCPT ); Thu, 3 Dec 2015 15:51:50 -0500 From: Rasmus Villemoes To: Andrew Morton , Kees Cook , Rasmus Villemoes Cc: linux-kernel@vger.kernel.org Subject: [PATCH v3 10/14] lib/test_printf.c: test precision quirks Date: Thu, 3 Dec 2015 21:51:09 +0100 Message-Id: <1449175873-1780-11-git-send-email-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.6.1 In-Reply-To: <1449175873-1780-1-git-send-email-linux@rasmusvillemoes.dk> References: <1449175873-1780-1-git-send-email-linux@rasmusvillemoes.dk> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Cc: Kees Cook Signed-off-by: Rasmus Villemoes --- lib/test_printf.c | 21 +++++++++++++++------ 1 file 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) 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"); } -- 2.6.1