Linux-Sparse Archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH] fix eval of the assignment of a non-restricted value to a restricted variable
Date: Thu, 25 Feb 2021 23:05:37 +0100	[thread overview]
Message-ID: <20210225220537.95335-1-luc.vanoostenryck@gmail.com> (raw)

Assignment to restricted variables are severely ... restricted.
Nevertheless, one value is always fine because it has always
the same bit representation: 0.

So, 0 is accepted unconditionally but this creates a problem
because the type of this 0 needs to be adjusted. Otherwise
0 (int) is assigned as-is even on restricted variable with a
different bit-length.

Fix this by casting the value to the target type before accepting it.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                             |  2 +-
 validation/eval/assign-restricted-ok.c | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 validation/eval/assign-restricted-ok.c

diff --git a/evaluate.c b/evaluate.c
index 41871e18503a..a661027f8df6 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1442,7 +1442,7 @@ static int check_assignment_types(struct symbol *target, struct expression **rp,
 			if (sclass & TYPE_FOULED && unfoul(s) == t)
 				goto Cast;
 			if (!restricted_value(*rp, target))
-				return 1;
+				goto Cast;
 			if (s == t)
 				return 1;
 		} else if (!(sclass & TYPE_RESTRICT))
diff --git a/validation/eval/assign-restricted-ok.c b/validation/eval/assign-restricted-ok.c
new file mode 100644
index 000000000000..df94d8c9d6d0
--- /dev/null
+++ b/validation/eval/assign-restricted-ok.c
@@ -0,0 +1,22 @@
+#ifdef __CHECKER__
+#define __bitwise __attribute__((bitwise))
+#else
+#define __bitwise
+#endif
+
+typedef __INT16_TYPE__ __bitwise __be16;
+
+static __be16 foo(void)
+{
+	__be16 val = 0;
+	return val;
+}
+
+/*
+ * check-name: assign-restricted-ok
+ * check-command: test-linearize -fdump-ir $file
+ *
+ * check-output-ignore
+ * check-output-contains: store\\.16
+ * check-output-excludes: store\\.32
+ */
-- 
2.30.0


                 reply	other threads:[~2021-02-25 22:06 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=20210225220537.95335-1-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.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
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 read-only IMAP folder(s) and NNTP newsgroup(s).