From patchwork Tue Oct 30 09:57:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Dooks X-Patchwork-Id: 10660583 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 E444014E2 for ; Tue, 30 Oct 2018 09:58:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC7B9296FD for ; Tue, 30 Oct 2018 09:58:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF19729702; Tue, 30 Oct 2018 09:58:03 +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 43E67296FD for ; Tue, 30 Oct 2018 09:58:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726985AbeJ3Sus (ORCPT ); Tue, 30 Oct 2018 14:50:48 -0400 Received: from imap1.codethink.co.uk ([176.9.8.82]:40821 "EHLO imap1.codethink.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726984AbeJ3Sus (ORCPT ); Tue, 30 Oct 2018 14:50:48 -0400 Received: from [148.252.241.226] (helo=rainbowdash) by imap1.codethink.co.uk with esmtpsa (Exim 4.84_2 #1 (Debian)) id 1gHQmc-0008Nx-UH; Tue, 30 Oct 2018 09:57:59 +0000 Received: from ben by rainbowdash with local (Exim 4.91) (envelope-from ) id 1gHQmc-0003mS-GI; Tue, 30 Oct 2018 09:57:58 +0000 From: Ben Dooks To: linux-sparse@vger.kernel.org Cc: Ben Dooks Subject: [PATCH] tests: add varargs printf format tests Date: Tue, 30 Oct 2018 09:57:57 +0000 Message-Id: <20181030095757.14489-1-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181029153952.13927-1-ben.dooks@codethink.co.uk> References: <20181029153952.13927-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 | 57 ++++++++++++++++++++++++++ validation/varargs-format-bad.c | 14 ++++++- validation/varargs-format-prefix.c | 18 ++++++++ validation/varargs-type-checking.c | 19 +++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 validation/varargs-format-addrspace1.c create mode 100644 validation/varargs-format-prefix.c create mode 100644 validation/varargs-type-checking.c diff --git a/validation/varargs-format-addrspace1.c b/validation/varargs-format-addrspace1.c new file mode 100644 index 0000000..55da817 --- /dev/null +++ b/validation/varargs-format-addrspace1.c @@ -0,0 +1,57 @@ + +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))); + + variadic(__msg, a, b); + ptr("hello %s %s", a, b); + ptr2("hello %s %s", a, b); +} + +/* + * check-name: variadic formatting test with addres-space to %s + * + * 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 string +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 string +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 string +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 string +varargs-format-addrspace1.c:14:36: got void [noderef] *a +varargs-format-addrspace1.c:15:36: warning: incorrect type in argument 4 (different address spaces) +varargs-format-addrspace1.c:15:36: expected void [noderef] * +varargs-format-addrspace1.c:15:36: got void [noderef] *a +varargs-format-addrspace1.c:26:25: warning: incorrect type in argument 2 (different address spaces) +varargs-format-addrspace1.c:26:25: expected string +varargs-format-addrspace1.c:26:25: 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 string +varargs-format-addrspace1.c:27:25: got void [noderef] *a + * check-error-end + */ diff --git a/validation/varargs-format-bad.c b/validation/varargs-format-bad.c index a8b9cdb..2b96aba 100644 --- a/validation/varargs-format-bad.c +++ b/validation/varargs-format-bad.c @@ -1,9 +1,19 @@ 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, 3))); +extern int variadic3(char *msg, int , ...) __attribute__((format (printf, 2, 1))); static void test(void) { - variadic3("test", 1); } +/* + * check-name: variadic formatting test with bad formatting parameters + * + * 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-prefix.c b/validation/varargs-format-prefix.c new file mode 100644 index 0000000..a91d1ba --- /dev/null +++ b/validation/varargs-format-prefix.c @@ -0,0 +1,18 @@ + +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-error-start +varargs-format-prefix.c:7:26: warning: incorrect type in argument 2 (different address spaces) +varargs-format-prefix.c:7:26: expected string +varargs-format-prefix.c:7:26: got void [noderef] *a + * check-error-end + */ diff --git a/validation/varargs-type-checking.c b/validation/varargs-type-checking.c new file mode 100644 index 0000000..1e289b2 --- /dev/null +++ b/validation/varargs-type-checking.c @@ -0,0 +1,19 @@ + +extern void pf(char *msg, ...) __attribute__((format (printf, 1, 2))); + +static void test(void) { + pf("%u %lu %llu\n", 1, 1UL, 1ULL); + pf("%d %ld %lld\n", 1, 1L, 1LL); + pf("%d %lx %llx\n", 1, 1L, 1LL); + pf("%d %ld %lld\n", 1, 1L, 1L); +} + +/* + * check-name: variadic formatting test type checking + * + * check-error-start +validation/varargs-type-checking.c:8:36: warning: incorrect type in argument 4 (different base types) +validation/varargs-type-checking.c:8:36: expected long long +validation/varargs-type-checking.c:8:36: got long + * check-error-end + */