From patchwork Wed Apr 3 15:35:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 10884029 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A76E817E0 for ; Wed, 3 Apr 2019 15:35:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9110428787 for ; Wed, 3 Apr 2019 15:35:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 85CDC287F4; Wed, 3 Apr 2019 15:35:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CEE6428787 for ; Wed, 3 Apr 2019 15:35:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726074AbfDCPf6 (ORCPT ); Wed, 3 Apr 2019 11:35:58 -0400 Received: from imap1.codethink.co.uk ([176.9.8.82]:51739 "EHLO imap1.codethink.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726155AbfDCPf6 (ORCPT ); Wed, 3 Apr 2019 11:35:58 -0400 Received: from [167.98.27.226] (helo=rainbowdash.codethink.co.uk) by imap1.codethink.co.uk with esmtpsa (Exim 4.84_2 #1 (Debian)) id 1hBhve-0005VM-Po; Wed, 03 Apr 2019 16:35:54 +0100 Received: from ben by rainbowdash.codethink.co.uk with local (Exim 4.92) (envelope-from ) id 1hBhve-00067j-2s; Wed, 03 Apr 2019 16:35:54 +0100 From: Ben Dooks To: linux-sparse@vger.kernel.org Cc: Ben Dooks Subject: [PATCH 5/6] tests: add varargs printf format tests Date: Wed, 3 Apr 2019 16:35:51 +0100 Message-Id: <20190403153552.23461-6-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190403153552.23461-1-ben.dooks@codethink.co.uk> References: <20190403153552.23461-1-ben.dooks@codethink.co.uk> MIME-Version: 1.0 Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add some tests for the new printf format checking code. Note, these do not all pass yet. Signed-off-by: Ben Dooks --- validation/varargs-format-addrspace1.c | 63 ++++++++++++++++++++++++++ validation/varargs-format-bad.c | 20 ++++++++ validation/varargs-format-checking.c | 20 ++++++++ validation/varargs-format-position.c | 31 +++++++++++++ validation/varargs-format-prefix.c | 19 ++++++++ validation/varargs-format-tests.c | 37 +++++++++++++++ 6 files changed, 190 insertions(+) create mode 100644 validation/varargs-format-addrspace1.c create mode 100644 validation/varargs-format-bad.c create mode 100644 validation/varargs-format-checking.c create mode 100644 validation/varargs-format-position.c create mode 100644 validation/varargs-format-prefix.c create mode 100644 validation/varargs-format-tests.c diff --git a/validation/varargs-format-addrspace1.c b/validation/varargs-format-addrspace1.c new file mode 100644 index 0000000..deb7ad3 --- /dev/null +++ b/validation/varargs-format-addrspace1.c @@ -0,0 +1,63 @@ + +extern int variadic(char *msg, ...) __attribute__((format (printf, 1, 2))); +extern int variadic2(char *msg, int , ...) __attribute__((format (printf, 1, 3))); +extern int variadic3(int, char *msg, ...) __attribute__((format (printf, 2, 3))); + +static void test(void) { + void __attribute__((noderef, address_space(1))) *a; + void *b; + + variadic("%s\n", a); + variadic("%s\n", b); + variadic("%s %s\n", b, a); + variadic2("%s %s\n", 1, b, a); + variadic3(1, "%s %s\n", b, a); + variadic3(1, "%s %p\n", b, a); +} + +static char __msg[] = "%s %p"; + +static void test2(void) { + void __attribute__((noderef, address_space(1))) *a; + void *b; + int (*ptr)(char *msg, ...) __attribute__((format (printf, 1, 2))) = variadic; + int (*ptr2)(char *msg, ...) __attribute__((format (printf, 1, 2))); + int (*ptr3)(int c, char *msg, ...) __attribute__((format (printf, 2, 3))); + + variadic(__msg, a, b); + ptr("hello %s %s", a, b); + ptr2("hello %s %s", a, b); + ptr3(1, "hello %s %s", a, b); +} + +/* + * check-name: variadic formatting test with address-space to %s + * check-command: sparse -Wformat $file + * + * check-error-start +varargs-format-addrspace1.c:10:26: warning: incorrect type in argument 2 (different address spaces) +varargs-format-addrspace1.c:10:26: expected char * +varargs-format-addrspace1.c:10:26: got void [noderef] *a +varargs-format-addrspace1.c:12:32: warning: incorrect type in argument 3 (different address spaces) +varargs-format-addrspace1.c:12:32: expected char * +varargs-format-addrspace1.c:12:32: got void [noderef] *a +varargs-format-addrspace1.c:13:36: warning: incorrect type in argument 4 (different address spaces) +varargs-format-addrspace1.c:13:36: expected char * +varargs-format-addrspace1.c:13:36: got void [noderef] *a +varargs-format-addrspace1.c:14:36: warning: incorrect type in argument 4 (different address spaces) +varargs-format-addrspace1.c:14:36: expected char * +varargs-format-addrspace1.c:14:36: got void [noderef] *a +varargs-format-addrspace1.c:27:25: warning: incorrect type in argument 2 (different address spaces) +varargs-format-addrspace1.c:27:25: expected char * +varargs-format-addrspace1.c:27:25: got void [noderef] *a +varargs-format-addrspace1.c:28:28: warning: incorrect type in argument 2 (different address spaces) +varargs-format-addrspace1.c:28:28: expected char * +varargs-format-addrspace1.c:28:28: got void [noderef] *a +varargs-format-addrspace1.c:29:29: warning: incorrect type in argument 2 (different address spaces) +varargs-format-addrspace1.c:29:29: expected char * +varargs-format-addrspace1.c:29:29: got void [noderef] *a +varargs-format-addrspace1.c:30:32: warning: incorrect type in argument 3 (different address spaces) +varargs-format-addrspace1.c:30:32: expected char * +varargs-format-addrspace1.c:30:32: got void [noderef] *a + * check-error-end + */ diff --git a/validation/varargs-format-bad.c b/validation/varargs-format-bad.c new file mode 100644 index 0000000..12b11f4 --- /dev/null +++ b/validation/varargs-format-bad.c @@ -0,0 +1,20 @@ + +extern int variadic(char *msg, ...) __attribute__((format (printf, 0, 0))); +extern int variadic2(char *msg, int , ...) __attribute__((format (printf, 2, 2))); +extern int variadic3(char *msg, int , ...) __attribute__((format (printf, 2, 1))); + +static void test(void) { +} + +/* + * check-name: variadic formatting test with bad formatting parameters + * check-command: sparse -Wformat $file + * + * check-error-start +varargs-format-bad.c:2:72: warning: bad format positions +varargs-format-bad.c:3:79: warning: bad format positions +varargs-format-bad.c:4:79: warning: format cannot be after va_args +* check-error-end + */ + + diff --git a/validation/varargs-format-checking.c b/validation/varargs-format-checking.c new file mode 100644 index 0000000..4444800 --- /dev/null +++ b/validation/varargs-format-checking.c @@ -0,0 +1,20 @@ + +extern void pf(char *msg, ...) __attribute__((format (printf, 1, 2))); + +static void test(void) { + pf("%u %lu %llu\n", 1U, 1UL, 1ULL); + pf("%d %ld %lld\n", 1, 1L, 1LL); + pf("%x %lx %llx\n", 1U, 1UL, 1ULL); + pf("%d %ld %lld\n", 1, 1L, 1L); +} + +/* + * check-name: variadic formatting test type checking + * check-command: sparse -Wformat $file + * + * check-error-start +varargs-format-checking.c:8:36: warning: incorrect type in argument 4 (different types) +varargs-format-checking.c:8:36: expected long long +varargs-format-checking.c:8:36: got long + * check-error-end + */ diff --git a/validation/varargs-format-position.c b/validation/varargs-format-position.c new file mode 100644 index 0000000..cb57f64 --- /dev/null +++ b/validation/varargs-format-position.c @@ -0,0 +1,31 @@ + +extern void pf(char *msg, ...) __attribute__((format (printf, 1, 2))); + +static void test(void) { + pf("%2$d %u\n", 1U, 1L); + pf("%3$d %2$u\n", 1U, 1); + pf("%1$d %2$d\n", 1L, 1); +} + +/* + * check-name: variadic formatting test position checking + * check-command: sparse -Wformat $file + * + * check-error-start +varargs-format-position.c:5:29: warning: incorrect type in argument 3 (different types) +varargs-format-position.c:5:29: expected int +varargs-format-position.c:5:29: got long +varargs-format-position.c:5:12: warning: format 3: position: no position specified +varargs-format-position.c:5:29: warning: incorrect type in argument 3 (different types) +varargs-format-position.c:5:29: expected unsigned int +varargs-format-position.c:5:29: got long +varargs-format-position.c:6:12: warning: no argument at position '4' +varargs-format-position.c:6:31: warning: incorrect type in argument 3 (different types) +varargs-format-position.c:6:31: expected unsigned int +varargs-format-position.c:6:31: got int +varargs-format-position.c:7:27: warning: incorrect type in argument 2 (different types) +varargs-format-position.c:7:27: expected int +varargs-format-position.c:7:27: got long + * check-error-end + * + */ diff --git a/validation/varargs-format-prefix.c b/validation/varargs-format-prefix.c new file mode 100644 index 0000000..ff0f0b8 --- /dev/null +++ b/validation/varargs-format-prefix.c @@ -0,0 +1,19 @@ + +extern int __attribute__((format (printf, 1, 2))) variadic(char *msg, ...); + +static int test(void) { + void __attribute__((noderef, address_space(1))) *a; + + variadic("%s\n", a); +} + +/* + * check-name: variadic formatting test prefix based __attribute__ + * check-command: sparse -Wformat $file + * + * check-error-start +varargs-format-prefix.c:7:26: warning: incorrect type in argument 2 (different address spaces) +varargs-format-prefix.c:7:26: expected char * +varargs-format-prefix.c:7:26: got void [noderef] *a + * check-error-end + */ diff --git a/validation/varargs-format-tests.c b/validation/varargs-format-tests.c new file mode 100644 index 0000000..3154ec1 --- /dev/null +++ b/validation/varargs-format-tests.c @@ -0,0 +1,37 @@ + +extern void pf(char *msg, ...) __attribute__((format (printf, 1, 2))); + +static int test(void) +{ + pf("%*d\n", 5, 10); /* value 10, print width is 5 */ + pf("%2$*1$d\n", 5, 10); /* value 10, print width is 5 */ + pf("%3$*2$d\n", 1, 5, 10); /* ok, skipping the '1' */ + pf("%3$-*2$d\n", 1, 5, 10); /* ok, skipping the '1' */ + pf("%3$*2$-d\n", 1, 5, 10); /* bad, the "-" shouldn't be before the 'd' */ + pf("%3$ *2$d\n", 1, 5, 10); /* ok, skipping the '1' */ + pf("%3$+*2$d\n", 1, 5, 10); /* ok, skipping the '1' */ + pf("%3$0+*2$d\n", 1, 5, 10); /* ok, skipping the '1' */ + pf("%3$+0*2$d\n", 1, 5, 10); /* ok, skipping the '1' */ + pf("%3$+#*2$d\n", 1, 5, 10); /* ok, skipping the '1' */ + pf("%3$+#*2$.5d\n", 1, 5, 10); /* ok, skipping the '1' */ + + /* go with some precision as well as width strings */ + pf("%2$+*1$.6d\n", 5, 10); /* ok */ + pf("%2$+*1$.*3$d\n", 5, 10, 6); /* ok */ + pf("%2$+*3$.*1$d\n", 6, 10, 5); /* ok */ + pf("%2$+*1$.*d\n", 5, 10, 6); /* not ok */ + + pf("%s", "msg"); + return 0; +} + +/* + * check-name: variadic formatting tests for width/precisions + * check-command: sparse -Wformat $file + * + * check-error-start +varargs-format-tests.c:10:12: warning: cannot evaluate type '%3$*2$-d' +varargs-format-tests.c:10:12: warning: cannot evaluate format string +varargs-format-tests.c:22:12: warning: format 3: position: no position specified + * check-error-end + */