public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] IBM zSystems: Fix left-shifting negative PCRel32 values (PR gas/29152)
@ 2022-05-16 18:35 Ilya Leoshkevich
  2022-05-16 20:01 ` Andreas Krebbel
  0 siblings, 1 reply; 2+ messages in thread
From: Ilya Leoshkevich @ 2022-05-16 18:35 UTC (permalink / raw)
  To: Andreas Krebbel; +Cc: Martin Liska, Alan Modra, binutils, Ilya Leoshkevich

s390_insert_operand ()'s val, min and max are encoded PCRel32 values
and need to be left-shifted by 1 before being shown to the user.
Left-shifting negative values is undefined behavior in C, but the
current code does not try to prevent it, causing UBSan to complain.

Fix by casting the values to their unsigned equivalents before
shifting.
---
 gas/config/tc-s390.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gas/config/tc-s390.c b/gas/config/tc-s390.c
index fb452f8a986..04a3c059c91 100644
--- a/gas/config/tc-s390.c
+++ b/gas/config/tc-s390.c
@@ -622,9 +622,9 @@ s390_insert_operand (unsigned char *insn,
 
 	  if (operand->flags & S390_OPERAND_PCREL)
 	    {
-	      val <<= 1;
-	      min <<= 1;
-	      max <<= 1;
+	      val = (offsetT) ((addressT) val << 1);
+	      min = (offsetT) ((addressT) min << 1);
+	      max = (offsetT) ((addressT) max << 1);
 	    }
 	  if (file == (char *) NULL)
 	    as_bad (err, (int64_t) val, (int64_t) min, (int64_t) max);
-- 
2.35.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] IBM zSystems: Fix left-shifting negative PCRel32 values (PR gas/29152)
  2022-05-16 18:35 [PATCH] IBM zSystems: Fix left-shifting negative PCRel32 values (PR gas/29152) Ilya Leoshkevich
@ 2022-05-16 20:01 ` Andreas Krebbel
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Krebbel @ 2022-05-16 20:01 UTC (permalink / raw)
  To: Ilya Leoshkevich; +Cc: Martin Liska, Alan Modra, binutils

On 5/16/22 20:35, Ilya Leoshkevich wrote:
> s390_insert_operand ()'s val, min and max are encoded PCRel32 values
> and need to be left-shifted by 1 before being shown to the user.
> Left-shifting negative values is undefined behavior in C, but the
> current code does not try to prevent it, causing UBSan to complain.
> 
> Fix by casting the values to their unsigned equivalents before
> shifting.
> ---
>  gas/config/tc-s390.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/gas/config/tc-s390.c b/gas/config/tc-s390.c
> index fb452f8a986..04a3c059c91 100644
> --- a/gas/config/tc-s390.c
> +++ b/gas/config/tc-s390.c
> @@ -622,9 +622,9 @@ s390_insert_operand (unsigned char *insn,
>  
>  	  if (operand->flags & S390_OPERAND_PCREL)
>  	    {
> -	      val <<= 1;
> -	      min <<= 1;
> -	      max <<= 1;
> +	      val = (offsetT) ((addressT) val << 1);
> +	      min = (offsetT) ((addressT) min << 1);
> +	      max = (offsetT) ((addressT) max << 1);
>  	    }
>  	  if (file == (char *) NULL)
>  	    as_bad (err, (int64_t) val, (int64_t) min, (int64_t) max);

Committed to master. Thanks!

Andreas

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-16 20:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-16 18:35 [PATCH] IBM zSystems: Fix left-shifting negative PCRel32 values (PR gas/29152) Ilya Leoshkevich
2022-05-16 20:01 ` Andreas Krebbel

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).