Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5361547pxj; Tue, 22 Jun 2021 22:51:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwOdYWaJt7i0C81Bne25wW5fvW4bq3zqEb/XTFZaoz6K10qmR8UkwO0L1o1IR3cRU/Tw1bt X-Received: by 2002:a02:c6d9:: with SMTP id r25mr597500jan.137.1624427519592; Tue, 22 Jun 2021 22:51:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624427519; cv=none; d=google.com; s=arc-20160816; b=XT9+OLC0G0oMHWwNA67i5wnbvfv7lendZJRpZZ7HmQtTf+jjHA5BUzFE7HW2scqrBP 1TLl8UbosOZwbpTSdZWkglCNYNhxC8rihpqL4V3zZBd18UPe/1GSXrEx6Gy1wPLlQ3VV au7ICKRrsGOjx8VH0iqXQxzMYl53XuzPb1kvBX6BKfPkHQEoHsH58F6GZI5e7G0HqzHh DjLQghq8EXYv4KsJMjOmA3LjxAqkwTfn7NAIpHn38AYv83dTkiO/TjoNZtbAj14eeTIq I3piO05RpEL1UkmqKMTIQS1XecyYr1/6SHomz3CYrxhe/6HoZdSbwE6dYf6Wz5og/vnM x0pQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=1a4HNcoTyOEg6/A1rxJNABqciv6nB6NEV3ckEHNY1wc=; b=oyiLjCIxro2TfI+an7wkQSYcD2+JcGihkYFVFTPIMUzWEzZMc7/uzhnK1uzGHL0KsI WbTjE3ZaaI55wbbyRIWFPaxdq9a7IACAlPJ6XokfpYslbjG/6GKvx2KTIeg49nRVMzsM P+E6xgPlxoj7G4uifoekDrwqVTfsJoeuIW3aWHq0wpYoG4Wygbg3y8z+ypx6KBm5dcsa em1H3KmG0g2BvbaJnWHZiKSlHHn0VqaTd7UBPux7DAQ7675jNIPkp4Ev3AIx9S5E4Qzc mR9nqSO5uLH7iUHbxlIYA0tnkDGD0LJwvspnRonl+z55HMmv+m+wLLX0/2NgkNt+yu5I YNkw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s4si1499298ilo.110.2021.06.22.22.51.48; Tue, 22 Jun 2021 22:51:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230053AbhFWFxO (ORCPT + 99 others); Wed, 23 Jun 2021 01:53:14 -0400 Received: from foss.arm.com ([217.140.110.172]:58054 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230039AbhFWFxM (ORCPT ); Wed, 23 Jun 2021 01:53:12 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 608B811D4; Tue, 22 Jun 2021 22:50:55 -0700 (PDT) Received: from entos-ampere-02.shanghai.arm.com (entos-ampere-02.shanghai.arm.com [10.169.214.103]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 388B83F694; Tue, 22 Jun 2021 22:50:49 -0700 (PDT) From: Jia He To: Petr Mladek , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , Jonathan Corbet , Alexander Viro , Linus Torvalds Cc: "Peter Zijlstra (Intel)" , Eric Biggers , "Ahmed S. Darwish" , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Matthew Wilcox , Christoph Hellwig , nd@arm.com, Jia He Subject: [PATCH v2 4/4] lib/test_printf.c: add test cases for '%pD' Date: Wed, 23 Jun 2021 13:50:11 +0800 Message-Id: <20210623055011.22916-5-justin.he@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210623055011.22916-1-justin.he@arm.com> References: <20210623055011.22916-1-justin.he@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org After the behaviour of specifier '%pD' is changed to print the full path of struct file, the related test cases are also updated. Given the full path string of '%pD' is prepended from the end of the scratch buffer, the check of "wrote beyond the nul-terminator" should be skipped for '%pD'. Parameterize the new using_scratch_space in __test(), do_test() and wrapper macros to skip the test case mentioned above, Signed-off-by: Jia He Reviewed-by: Andy Shevchenko --- lib/test_printf.c | 49 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/lib/test_printf.c b/lib/test_printf.c index d1d2f898ebae..f48da88bc77b 100644 --- a/lib/test_printf.c +++ b/lib/test_printf.c @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -37,8 +38,8 @@ static char *alloced_buffer __initdata; extern bool no_hash_pointers; -static int __printf(4, 0) __init -do_test(int bufsize, const char *expect, int elen, +static int __printf(5, 0) __init +do_test(int bufsize, const char *expect, int elen, bool using_scratch_space, const char *fmt, va_list ap) { va_list aq; @@ -78,7 +79,7 @@ do_test(int bufsize, const char *expect, int elen, return 1; } - if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) { + if (!using_scratch_space && memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) { pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n", bufsize, fmt); return 1; @@ -97,8 +98,9 @@ do_test(int bufsize, const char *expect, int elen, return 0; } -static void __printf(3, 4) __init -__test(const char *expect, int elen, const char *fmt, ...) +static void __printf(4, 5) __init +__test(const char *expect, int elen, bool using_scratch_space, + const char *fmt, ...) { va_list ap; int rand; @@ -119,11 +121,11 @@ __test(const char *expect, int elen, const char *fmt, ...) * enough and 0), and then we also test that kvasprintf would * be able to print it as expected. */ - failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap); + failed_tests += do_test(BUF_SIZE, expect, elen, using_scratch_space, fmt, ap); rand = 1 + prandom_u32_max(elen+1); /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */ - failed_tests += do_test(rand, expect, elen, fmt, ap); - failed_tests += do_test(0, expect, elen, fmt, ap); + failed_tests += do_test(rand, expect, elen, using_scratch_space, fmt, ap); + failed_tests += do_test(0, expect, elen, using_scratch_space, fmt, ap); p = kvasprintf(GFP_KERNEL, fmt, ap); if (p) { @@ -138,8 +140,15 @@ __test(const char *expect, int elen, const char *fmt, ...) va_end(ap); } +/* + * More relaxed test for non-standard formats that are using the provided buffer + * as a scratch space and write beyond the trailing '\0'. + */ +#define test_using_scratch_space(expect, fmt, ...) \ + __test(expect, strlen(expect), true, fmt, ##__VA_ARGS__) + #define test(expect, fmt, ...) \ - __test(expect, strlen(expect), fmt, ##__VA_ARGS__) + __test(expect, strlen(expect), false, fmt, ##__VA_ARGS__) static void __init test_basic(void) @@ -150,7 +159,7 @@ test_basic(void) test("", &nul); test("100%", "100%%"); test("xxx%yyy", "xxx%cyyy", '%'); - __test("xxx\0yyy", 7, "xxx%cyyy", '\0'); + __test("xxx\0yyy", 7, false, "xxx%cyyy", '\0'); } static void __init @@ -501,6 +510,25 @@ dentry(void) test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]); } +static struct vfsmount test_vfsmnt __initdata = {}; + +static struct file test_file __initdata = { + .f_path = { .dentry = &test_dentry[2], + .mnt = &test_vfsmnt, + }, +}; + +static void __init +f_d_path(void) +{ + test("(null)", "%pD", NULL); + test("(efault)", "%pD", PTR_INVALID); + + test_using_scratch_space("/bravo/alfa |/bravo/alfa ", "%-14pD|%*pD", &test_file, -14, &test_file); + test_using_scratch_space(" /bravo/alfa| /bravo/alfa", "%14pD|%*pD", &test_file, 14, &test_file); + test_using_scratch_space(" /bravo/alfa|/bravo/alfa ", "%14pD|%-14pD", &test_file, &test_file); +} + static void __init struct_va_format(void) { @@ -784,6 +812,7 @@ test_pointer(void) ip(); uuid(); dentry(); + f_d_path(); struct_va_format(); time_and_date(); struct_clk(); -- 2.17.1