public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: liuzhensong <liuzhensong@loongson.cn>
To: binutils@sourceware.org
Cc: liuzhensong <liuzhensong@loongson.cn>
Subject: [PATCH] ubsan: loongarch : signed integer shift overflow.
Date: Sat, 12 Mar 2022 15:40:39 +0800	[thread overview]
Message-ID: <20220312074039.3808325-1-liuzhensong@loongson.cn> (raw)

   opcodes/
	* loongarch-coder.c :
	  int32_t ret = 0;
	  ret <<= sizeof (ret) * 8 - len;
	  ret >>= sizeof (ret) * 8 - len;
	  ...
	  Avoid ubsan warning.
---
 opcodes/loongarch-coder.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/opcodes/loongarch-coder.c b/opcodes/loongarch-coder.c
index cb6d5648da0..c957b95df74 100644
--- a/opcodes/loongarch-coder.c
+++ b/opcodes/loongarch-coder.c
@@ -106,11 +106,13 @@ loongarch_decode_imm (const char *bit_field, insn_t insn, int si)
   else if (*bit_field_1 == '+')
     ret += atoi (bit_field_1 + 1);
 
+  /* Extend signed bit.  */
   if (si)
     {
-      ret <<= sizeof (ret) * 8 - len;
-      ret >>= sizeof (ret) * 8 - len;
+      uint32_t sign = 1u << (len - 1);
+      ret = (ret ^ sign) - sign;
     }
+
   return ret;
 }
 
@@ -133,7 +135,8 @@ loongarch_encode_imm (const char *bit_field, int32_t imm)
   else if (*t == '+')
     uimm -= atoi (t + 1);
 
-  uimm <<= sizeof (uimm) * 8 - width;
+  uimm = width ? (uimm << (sizeof (uimm) * 8 - width)) : 0;
+
   while (1)
     {
       b_start = strtol (bit_field_1, &bit_field_1, 10);
@@ -141,10 +144,10 @@ loongarch_encode_imm (const char *bit_field, int32_t imm)
 	break;
       width = strtol (bit_field_1 + 1, &bit_field_1, 10);
       i = uimm;
-      i >>= sizeof (i) * 8 - width;
-      i <<= b_start;
+      i = width ? (i >> (sizeof (i) * 8 - width)) : 0;
+      i = (b_start == 32) ? 0 : (i << b_start);
       ret |= i;
-      uimm <<= width;
+      uimm = (width == 32) ? 0 : (uimm << width);
 
       if (*bit_field_1 != '|')
 	break;
-- 
2.31.1


                 reply	other threads:[~2022-03-12  7:40 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=20220312074039.3808325-1-liuzhensong@loongson.cn \
    --to=liuzhensong@loongson.cn \
    --cc=binutils@sourceware.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).