linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <lucvoo@kernel.org>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH] fix infinite loop when expanding __builtin_object_size() with self-init vars
Date: Sat, 21 May 2022 11:49:59 +0200	[thread overview]
Message-ID: <20220521094959.48865-1-lucvoo@kernel.org> (raw)

From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>

expand_object_size(), used to expand __builtin_object_size(),
recursively try to get the parent initializer. This fails miserably
by looping endlessly when the object is a self-initialized variable.

For the moment, fix this in the most obvious way: stop the recursion
and do not expand such variables.

Note: I wouldn't be surprised if these self-initialized variables create
      other problems elsewhere. Maybe we should remove their initializer
      and somehow mark them as "do not warn about -Wuninitialized"
      (well, there is no such warnings *yet*).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 builtin.c                              |  8 ++++++++
 validation/builtin-objsize-self-init.c | 11 +++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 validation/builtin-objsize-self-init.c

diff --git a/builtin.c b/builtin.c
index 8e1d2d7e9386..3a29c3aec8a6 100644
--- a/builtin.c
+++ b/builtin.c
@@ -546,11 +546,19 @@ static int expand_object_size(struct expression *expr, int cost)
 			// a deref is just intermediate variable
 			// and so the offset needs to be zeroed.
 			if (arg->op == '*') {
+				struct expression *parent = arg;
 				arg = arg->unop;
 				off = 0;
 				switch (arg->type) {
 				case EXPR_SYMBOL:
 					arg = arg->symbol->initializer;
+					if (arg == parent) {
+						// stop at self-initialized vars
+						// and do not expand them.
+						arg = NULL;
+						val = -1;
+						break;
+					}
 					continue;
 				default:
 					break;
diff --git a/validation/builtin-objsize-self-init.c b/validation/builtin-objsize-self-init.c
new file mode 100644
index 000000000000..77e3da43e6d2
--- /dev/null
+++ b/validation/builtin-objsize-self-init.c
@@ -0,0 +1,11 @@
+static void f(void)
+{
+	void *param = param;
+	__builtin_object_size(param, 0);
+}
+
+/*
+ * check-name: builtin-objsize-self-init
+ * check-timeout:
+ * check-error-end
+ */
-- 
2.36.1


                 reply	other threads:[~2022-05-21  9:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220521094959.48865-1-lucvoo@kernel.org \
    --to=lucvoo@kernel.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=luc.vanoostenryck@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).