public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* tic4x-coff: ubsan: various shift UB
@ 2020-09-01  6:47 Alan Modra
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2020-09-01  6:47 UTC (permalink / raw)
  To: binutils

	* config/tc-tic4x.c (tic4x_gen_to_words): Rewrite mantissa
	overflow test without UB.  Avoid other UB shifts by making them
	unsigned.

diff --git a/gas/config/tc-tic4x.c b/gas/config/tc-tic4x.c
index 1889b47128..4efb9d4ae5 100644
--- a/gas/config/tc-tic4x.c
+++ b/gas/config/tc-tic4x.c
@@ -453,8 +453,8 @@ tic4x_gen_to_words (FLONUM_TYPE flonum, LITTLENUM_TYPE *words, int precision)
   /* Store the mantissa data into smant and the roundbit into rbit */
   for (p = flonum.leader; p >= flonum.low && shift > -16; p--)
     {
-      tmp = shift >= 0 ? *p << shift : *p >> -shift;
-      rbit = shift < 0 ? ((*p >> (-shift-1)) & 0x1) : 0;
+      tmp = shift >= 0 ? (unsigned) *p << shift : (unsigned) *p >> -shift;
+      rbit = shift < 0 ? (((unsigned) *p >> (-shift-1)) & 0x1) : 0;
       smant |= tmp;
       shift -= 16;
     }
@@ -463,18 +463,14 @@ tic4x_gen_to_words (FLONUM_TYPE flonum, LITTLENUM_TYPE *words, int precision)
   if(rbit)
     {
       /* If the mantissa is going to overflow when added, lets store
-         the extra bit in mover. -- A special case exists when
-         mantissa_bits is 31 (E_PRECISION). Then the first test cannot
-         be trusted, as result is host-dependent, thus the second
-         test. */
-      if( smant == ((unsigned)(1<<(mantissa_bits+1))-1)
-          || smant == (unsigned)-1 )  /* This is to catch E_PRECISION cases */
+	 the extra bit in mover.  */
+      if (smant == (1u << mantissa_bits << 1) - 1)
         mover=1;
       smant++;
     }
 
   /* Get the scaled one value */
-  sone = (1 << (mantissa_bits));
+  sone = 1u << mantissa_bits;
 
   /* The number may be unnormalised so renormalise it...  */
   if(mover)
@@ -527,7 +523,7 @@ tic4x_gen_to_words (FLONUM_TYPE flonum, LITTLENUM_TYPE *words, int precision)
   else
     {
       /* Insert the exponent data into the word */
-      sfract |= exponent << (mantissa_bits+1);
+      sfract |= (unsigned) exponent << (mantissa_bits + 1);
 
       if (precision == S_PRECISION)
         words[0] = sfract;

-- 
Alan Modra
Australia Development Lab, IBM

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

* tic4x-coff: ubsan: various shift UB
@ 2020-08-31 10:50 Alan Modra
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2020-08-31 10:50 UTC (permalink / raw)
  To: binutils

	* config/tc-tic4x.c (tic4x_gen_to_words): Rewrite mantissa
	overflow test without UB.  Avoid other UB shifts by making them
	unsigned.

diff --git a/gas/config/tc-tic4x.c b/gas/config/tc-tic4x.c
index 1889b47128..4efb9d4ae5 100644
--- a/gas/config/tc-tic4x.c
+++ b/gas/config/tc-tic4x.c
@@ -453,8 +453,8 @@ tic4x_gen_to_words (FLONUM_TYPE flonum, LITTLENUM_TYPE *words, int precision)
   /* Store the mantissa data into smant and the roundbit into rbit */
   for (p = flonum.leader; p >= flonum.low && shift > -16; p--)
     {
-      tmp = shift >= 0 ? *p << shift : *p >> -shift;
-      rbit = shift < 0 ? ((*p >> (-shift-1)) & 0x1) : 0;
+      tmp = shift >= 0 ? (unsigned) *p << shift : (unsigned) *p >> -shift;
+      rbit = shift < 0 ? (((unsigned) *p >> (-shift-1)) & 0x1) : 0;
       smant |= tmp;
       shift -= 16;
     }
@@ -463,18 +463,14 @@ tic4x_gen_to_words (FLONUM_TYPE flonum, LITTLENUM_TYPE *words, int precision)
   if(rbit)
     {
       /* If the mantissa is going to overflow when added, lets store
-         the extra bit in mover. -- A special case exists when
-         mantissa_bits is 31 (E_PRECISION). Then the first test cannot
-         be trusted, as result is host-dependent, thus the second
-         test. */
-      if( smant == ((unsigned)(1<<(mantissa_bits+1))-1)
-          || smant == (unsigned)-1 )  /* This is to catch E_PRECISION cases */
+	 the extra bit in mover.  */
+      if (smant == (1u << mantissa_bits << 1) - 1)
         mover=1;
       smant++;
     }
 
   /* Get the scaled one value */
-  sone = (1 << (mantissa_bits));
+  sone = 1u << mantissa_bits;
 
   /* The number may be unnormalised so renormalise it...  */
   if(mover)
@@ -527,7 +523,7 @@ tic4x_gen_to_words (FLONUM_TYPE flonum, LITTLENUM_TYPE *words, int precision)
   else
     {
       /* Insert the exponent data into the word */
-      sfract |= exponent << (mantissa_bits+1);
+      sfract |= (unsigned) exponent << (mantissa_bits + 1);
 
       if (precision == S_PRECISION)
         words[0] = sfract;

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

end of thread, other threads:[~2020-09-01  6:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-01  6:47 tic4x-coff: ubsan: various shift UB Alan Modra
  -- strict thread matches above, loose matches on Subject: below --
2020-08-31 10:50 Alan Modra

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