public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-6823] c++: Avoid UB in signed shift [PR 98625]
@ 2021-01-20 19:42 Nathan Sidwell
  0 siblings, 0 replies; only message in thread
From: Nathan Sidwell @ 2021-01-20 19:42 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:911f797a9be2dc8ef59f5d5bd6d68baf650b8822

commit r11-6823-g911f797a9be2dc8ef59f5d5bd6d68baf650b8822
Author: Nathan Sidwell <nathan@acm.org>
Date:   Wed Jan 20 09:21:02 2021 -0800

    c++: Avoid UB in signed shift [PR 98625]
    
    I'd forgotten that left shifting a negative value is UB until C++20.
    Insert some casts to do unsigned shifts.
    
            PT c++/98625
            gcc/cp/
            * module.cc (bytes_in::i, bytes_in::wi): Avoid left shift of
            signed type.

Diff:
---
 gcc/cp/module.cc | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 3b40c7ead05..7f88e3fea89 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -878,9 +878,12 @@ bytes_in::i ()
 	  v &= 0xf;
 	  if (v & 0x8)
 	    v |= -1 ^ 0x7;
+	  /* unsigned necessary due to left shifts of -ve values.  */
+	  unsigned uv = unsigned (v);
 	  if ((ptr = read (++bytes)))
 	    while (bytes--)
-	      v = (v << 8) | (*ptr++ & 0xff);
+	      uv = (uv << 8) | (*ptr++ & 0xff);
+	  v = int (uv);
 	}
       else if (v & 0x40)
 	v |= -1 ^ 0x3f;
@@ -969,9 +972,12 @@ bytes_in::wi ()
 	  v &= 0xf;
 	  if (v & 0x8)
 	    v |= -1 ^ 0x7;
+	  /* unsigned necessary due to left shifts of -ve values.  */
+	  unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
 	  if ((ptr = read (++bytes)))
 	    while (bytes--)
-	      v = (v << 8) | (*ptr++ & 0xff);
+	      uv = (uv << 8) | (*ptr++ & 0xff);
+	  v = (HOST_WIDE_INT) uv;
 	}
       else if (v & 0x40)
 	v |= -1 ^ 0x3f;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-01-20 19:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-20 19:42 [gcc r11-6823] c++: Avoid UB in signed shift [PR 98625] Nathan Sidwell

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