public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-8423] convert: Fix test for out of bounds shift count [PR113574]
Date: Thu, 25 Jan 2024 12:16:03 +0000 (GMT)	[thread overview]
Message-ID: <20240125121603.7D2A23858C53@sourceware.org> (raw)

https://gcc.gnu.org/g:fb1b7e2fec951ba0bf4f68fac6a16929f4f63910

commit r14-8423-gfb1b7e2fec951ba0bf4f68fac6a16929f4f63910
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Jan 25 13:15:23 2024 +0100

    convert: Fix test for out of bounds shift count [PR113574]
    
    The following patch is miscompiled, because convert_to_integer_1 for
    LSHIFT_EXPR tests if the INTEGER_CST shift count is too high, but
    incorrectly compares it against TYPE_SIZE rather than TYPE_PRECISION.
    The type in question is unsigned _BitInt(1), which has TYPE_PRECISION 1,
    TYPE_SIZE 8, and the shift count is 2 in that case.
    
    2024-01-25  Jakub Jelinek  <jakub@redhat.com>
    
            PR middle-end/113574
            * convert.cc (convert_to_integer_1) <case LSHIFT_EXPR>: Compare shift
            count against TYPE_PRECISION rather than TYPE_SIZE.
    
            * gcc.dg/torture/bitint-52.c: New test.

Diff:
---
 gcc/convert.cc                           |  3 ++-
 gcc/testsuite/gcc.dg/torture/bitint-52.c | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/gcc/convert.cc b/gcc/convert.cc
index 179f5e69008..5ad24ad2c08 100644
--- a/gcc/convert.cc
+++ b/gcc/convert.cc
@@ -762,7 +762,8 @@ convert_to_integer_1 (tree type, tree expr, bool dofold)
 	      {
 		/* If shift count is less than the width of the truncated type,
 		   really shift.  */
-		if (tree_int_cst_lt (TREE_OPERAND (expr, 1), TYPE_SIZE (type)))
+		if (wi::to_widest (TREE_OPERAND (expr, 1))
+		    < TYPE_PRECISION (type))
 		  /* In this case, shifting is like multiplication.  */
 		  goto trunc1;
 		else
diff --git a/gcc/testsuite/gcc.dg/torture/bitint-52.c b/gcc/testsuite/gcc.dg/torture/bitint-52.c
new file mode 100644
index 00000000000..d2896da2f9a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/bitint-52.c
@@ -0,0 +1,23 @@
+/* PR middle-end/113574 */
+/* { dg-do run { target bitint } } */
+/* { dg-options "-std=c23 -pedantic-errors" } */
+/* { dg-skip-if "" { ! run_expensive_tests }  { "*" } { "-O0" "-O2" } } */
+/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */
+
+unsigned _BitInt(1) a;
+unsigned _BitInt(8) b;
+
+void
+foo (unsigned _BitInt(16) x)
+{
+  a += (x << 2) | b;
+}
+
+int
+main ()
+{
+  foo (0xfef1uwb);
+  if (a)
+    __builtin_abort ();
+  return 0;
+}

                 reply	other threads:[~2024-01-25 12:16 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=20240125121603.7D2A23858C53@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.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).