diff mbox series

[5/5] tests: add varargs printf format tests

Message ID 20181101181117.3877-6-ben.dooks@codethink.co.uk (mailing list archive)
State Superseded, archived
Headers show
Series [1/5] ptrlist: add ptr_list_nth_entry() | expand

Commit Message

Ben Dooks Nov. 1, 2018, 6:11 p.m. UTC
Add some tests for the new printf format checking code.
Note, these do not all pass yet.

Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
---
 validation/varargs-format-addrspace1.c | 60 ++++++++++++++++++++++++++
 validation/varargs-format-bad.c        | 14 +++++-
 validation/varargs-format-checking.c   | 19 ++++++++
 validation/varargs-format-position.c   | 29 +++++++++++++
 validation/varargs-format-prefix.c     | 18 ++++++++
 validation/varargs-format-tests.c      | 35 +++++++++++++++
 6 files changed, 173 insertions(+), 2 deletions(-)
 create mode 100644 validation/varargs-format-addrspace1.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

Comments

Luc Van Oostenryck Nov. 1, 2018, 10:16 p.m. UTC | #1
On Thu, Nov 01, 2018 at 06:11:17PM +0000, Ben Dooks wrote:
> Add some tests for the new printf format checking code.
> Note, these do not all pass yet.
> 
> Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
> ---
>  validation/varargs-format-addrspace1.c | 60 ++++++++++++++++++++++++++
>  validation/varargs-format-bad.c        | 14 +++++-
>  validation/varargs-format-checking.c   | 19 ++++++++
>  validation/varargs-format-position.c   | 29 +++++++++++++
>  validation/varargs-format-prefix.c     | 18 ++++++++
>  validation/varargs-format-tests.c      | 35 +++++++++++++++
>  6 files changed, 173 insertions(+), 2 deletions(-)
>  create mode 100644 validation/varargs-format-addrspace1.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-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);
>  }

Mmmm, I don't have the patch where this file is created.
It was already so in the previous version.


Kind regards,
-- Luc
diff mbox series

Patch

diff --git a/validation/varargs-format-addrspace1.c b/validation/varargs-format-addrspace1.c
new file mode 100644
index 0000000..3c1f37a
--- /dev/null
+++ b/validation/varargs-format-addrspace1.c
@@ -0,0 +1,60 @@ 
+
+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] <asn:1>*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] <asn:1>*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] <asn:1>*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] <asn:1>*a
+varargs-format-addrspace1.c:15:36: warning: incorrect type in argument 4 (different address spaces)
+varargs-format-addrspace1.c:15:36:    expected void *
+varargs-format-addrspace1.c:15:36:    got void [noderef] <asn:1>*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] <asn:1>*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] <asn:1>*a
+varargs-format-addrspace1.c:28:25: warning: incorrect type in argument 2 (different address spaces)
+varargs-format-addrspace1.c:28:25:    expected string
+varargs-format-addrspace1.c:28:25:    got void [noderef] <asn:1>*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-checking.c b/validation/varargs-format-checking.c
new file mode 100644
index 0000000..672b497
--- /dev/null
+++ b/validation/varargs-format-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", 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-error-start
+varargs-type-checking.c:8:36: warning: incorrect type in argument 4 (different types)
+varargs-type-checking.c:8:36:    expected long long
+varargs-type-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..4473d47
--- /dev/null
+++ b/validation/varargs-format-position.c
@@ -0,0 +1,29 @@ 
+
+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-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: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..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] <asn:1>*a
+ * check-error-end
+ */
diff --git a/validation/varargs-format-tests.c b/validation/varargs-format-tests.c
new file mode 100644
index 0000000..e2b8196
--- /dev/null
+++ b/validation/varargs-format-tests.c
@@ -0,0 +1,35 @@ 
+
+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);	/* ok, skipping the '1' (breaks) */
+	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-error-start
+varargs-format-tests.c:9:32: warning: cannot evaluate type '3$*2$-d'
+varargs-format-tests.c:22:12: warning: format 3: position: no position specified
+ * check-error-end
+ */