Linux-Sparse Archive on lore.kernel.org
 help / color / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 01/13] flex-array: add testcases
Date: Thu,  1 Oct 2020 01:18:16 +0200
Message-ID: <20200930231828.66751-2-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20200930231828.66751-1-luc.vanoostenryck@gmail.com>

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 validation/flex-array-align.c   | 19 +++++++++++++++++++
 validation/flex-array-array.c   | 16 ++++++++++++++++
 validation/flex-array-error.c   | 27 +++++++++++++++++++++++++++
 validation/flex-array-nested.c  | 30 ++++++++++++++++++++++++++++++
 validation/flex-array-padding.c | 22 ++++++++++++++++++++++
 validation/flex-array-sizeof.c  | 19 +++++++++++++++++++
 6 files changed, 133 insertions(+)
 create mode 100644 validation/flex-array-align.c
 create mode 100644 validation/flex-array-array.c
 create mode 100644 validation/flex-array-error.c
 create mode 100644 validation/flex-array-nested.c
 create mode 100644 validation/flex-array-padding.c
 create mode 100644 validation/flex-array-sizeof.c

diff --git a/validation/flex-array-align.c b/validation/flex-array-align.c
new file mode 100644
index 000000000000..0cc67ab36997
--- /dev/null
+++ b/validation/flex-array-align.c
@@ -0,0 +1,19 @@
+struct s {
+	__INT32_TYPE__ x;
+	__INT16_TYPE__ y;
+	unsigned char f[];
+};
+
+static int foo(struct s *s)
+{
+	return (sizeof(*s) << 16) | __builtin_offsetof(typeof(*s), f);
+}
+
+/*
+ * check-name: flex-array-align
+ * check-command: test-linearize -Wno-flexible-array-sizeof $file
+ * check-known-to-fail
+ *
+ * check-output-ignore
+ * check-output-contains: ret\\..*\\$0x80006
+ */
diff --git a/validation/flex-array-array.c b/validation/flex-array-array.c
new file mode 100644
index 000000000000..bda80d7a0a20
--- /dev/null
+++ b/validation/flex-array-array.c
@@ -0,0 +1,16 @@
+struct s {
+	int i;
+	long f[];
+};
+
+static struct s a[2];
+
+/*
+ * check-name: flex-array-array
+ * check-command: sparse -Wflexible-array-array $file
+ * check-known-to-fail
+ *
+ * check-error-start
+flex-array-array.c:6:18: warning: array of flexible structures
+ * check-error-end
+ */
diff --git a/validation/flex-array-error.c b/validation/flex-array-error.c
new file mode 100644
index 000000000000..89601e42daf6
--- /dev/null
+++ b/validation/flex-array-error.c
@@ -0,0 +1,27 @@
+struct s {
+	int i;
+	long f[];
+	int j;
+};
+
+union u {
+	int i;
+	long f[];
+};
+
+// trigger the examination of the offending types
+static int foo(struct s *s, union u *u)
+{
+	return    __builtin_offsetof(typeof(*s), i)
+		+ __builtin_offsetof(typeof(*u), i);
+}
+
+/*
+ * check-name: flex-array-error
+ * check-known-to-fail
+ *
+ * check-error-start
+flex-array-error.c:3:14: error: flexible array member 'f' is not last
+flex-array-error.c:9:14: error: flexible array member 'f' in a union
+ * check-error-end
+ */
diff --git a/validation/flex-array-nested.c b/validation/flex-array-nested.c
new file mode 100644
index 000000000000..3503c329d7c3
--- /dev/null
+++ b/validation/flex-array-nested.c
@@ -0,0 +1,30 @@
+struct f {
+	int i;
+	long f[];
+};
+
+struct s {
+	struct f f;
+};
+
+union u {
+	struct f f;
+};
+
+// trigger the examination of the offending types
+static int foo(struct s *s, union u *u)
+{
+	return    __builtin_offsetof(typeof(*s), f)
+		+ __builtin_offsetof(typeof(*u), f);
+}
+
+/*
+ * check-name: flex-array-nested
+ * check-command: sparse -Wflexible-array-nested $file
+ * check-known-to-fail
+ *
+ * check-error-start
+flex-array-nested.c:6:8: warning: nested flexible arrays
+flex-array-nested.c:10:7: warning: nested flexible arrays
+ * check-error-end
+ */
diff --git a/validation/flex-array-padding.c b/validation/flex-array-padding.c
new file mode 100644
index 000000000000..2ba77971266e
--- /dev/null
+++ b/validation/flex-array-padding.c
@@ -0,0 +1,22 @@
+struct s {
+	__INT32_TYPE__ x;
+	__INT16_TYPE__ y;
+	unsigned char f[];
+};
+
+static int foo(struct s *s)
+{
+	return __builtin_offsetof(typeof(*s), f);
+}
+
+/*
+ * check-name: flex-array-padding
+ * check-command: test-linearize -Wflexible-array-padding $file
+ * check-known-to-fail
+ *
+ * check-output-ignore
+ *
+ * check-error-start
+flex-array-padding.c:4:23: warning: flexible array member has padding
+ * check-error-end
+ */
diff --git a/validation/flex-array-sizeof.c b/validation/flex-array-sizeof.c
new file mode 100644
index 000000000000..3359509d0084
--- /dev/null
+++ b/validation/flex-array-sizeof.c
@@ -0,0 +1,19 @@
+struct s {
+	int i;
+	long f[];
+};
+
+static int foo(struct s *s)
+{
+	return sizeof(*s);
+}
+
+/*
+ * check-name: flex-array-sizeof
+ * check-command: sparse -Wflexible-array-sizeof $file
+ * check-known-to-fail
+ *
+ * check-error-start
+flex-array-sizeof.c:8:16: warning: using sizeof on a flexible structure
+ * check-error-end
+ */
-- 
2.28.0


  reply index

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-30 23:18 [PATCH 00/13] add warnings for flexible arrays Luc Van Oostenryck
2020-09-30 23:18 ` Luc Van Oostenryck [this message]
2020-09-30 23:18 ` [PATCH 02/13] flex-array: factor out common part of lay_out_{struct,union}() Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 03/13] flex-array: do not lay out invalid struct members Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 04/13] flex-array: flexible array members have zero size and alignment is OK Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 05/13] flex-array: detect structures with a flexible array member Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 06/13] flex-array: warn on flexible arrays in unions Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 07/13] flex-array: warn if flexible array is not last Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 08/13] flex-array: identify structures with a flexible array member Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 09/13] flex-array: add helper has_flexible_array() Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 10/13] flex-array: warn when using sizeof() on a flexible array Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 11/13] flex-array: warn an arrays containing " Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 12/13] flex-array: warn on flexible array in nested aggregate types Luc Van Oostenryck
2020-09-30 23:18 ` [PATCH 13/13] flex-array: warn when a flexible array member has some padding Luc Van Oostenryck
2020-10-01 16:34   ` Linus Torvalds
2020-10-01 19:17     ` Luc Van Oostenryck
2020-10-01 19:27       ` Linus Torvalds
2020-10-01 19:41         ` Luc Van Oostenryck
2020-10-01 19:51           ` Luc Van Oostenryck
2020-10-01 16:36 ` [PATCH 00/13] add warnings for flexible arrays Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200930231828.66751-2-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Sparse Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-sparse/0 linux-sparse/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-sparse linux-sparse/ https://lore.kernel.org/linux-sparse \
		linux-sparse@vger.kernel.org
	public-inbox-index linux-sparse

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-sparse


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git