diff mbox

RFA: Libiberty: Fix warnings about left shifting a negative value.

Message ID 87h9jgj6i2.fsf@redhat.com
State New
Headers show

Commit Message

Nick Clifton Dec. 18, 2015, 12:17 p.m. UTC
Hi DJ, Hi Ian,

  GCC PR 66827 reports some problems with left shifting a negative
  value:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66827

  Of the problems reported only two remain - in libiberty/regex.c:
    
libiberty/regex.c:6970:11: runtime error: left shift of negative value -1
libiberty/regex.c:7165:4: runtime error: left shift of negative value -1

  The patch below fixes these errors by casting the value to be shifted
  to unsigned before the shift occurs.

  No regressions were found in the libiberty testsuite or bootstrapping
  gcc (on an x86_64 target).

  OK to apply ?

Cheers
  Nick

PS: Running the libiberty testsuite with -fsanitize=undefined does throw
  up a couple more runtime errors:

libiberty/cplus-dem.c:503:13: runtime error: signed integer overflow: 922337203 * 10 cannot be represented in type 'int'
libiberty/cp-demangle.c:4123:40: runtime error: variable length array bound evaluates to non-positive value 0
libiberty/cp-demangle.c:4124:43: runtime error: variable length array bound evaluates to non-positive value 0

  I have not attempted to fix these.  The first looks like it is a
  deliberate piece of coding and the other two look unimportant since
  the zero sized arrays will never be used. 

libiberty/ChangeLog
2015-12-18  Nick Clifton  <nickc@redhat.com>

	PR 66827
	* regex.c (EXTRACT_NUMBER): Cast sign byte to unsigned before left
	shifting.

Comments

Ian Lance Taylor Dec. 19, 2015, 2:21 a.m. UTC | #1
On Fri, Dec 18, 2015 at 4:17 AM, Nick Clifton <nickc@redhat.com> wrote:
>
> 2015-12-18  Nick Clifton  <nickc@redhat.com>
>
>         PR 66827
>         * regex.c (EXTRACT_NUMBER): Cast sign byte to unsigned before left
>         shifting.

This is OK.

Thanks.

Ian
diff mbox

Patch

Index: libiberty/regex.c
===================================================================
--- libiberty/regex.c	(revision 231805)
+++ libiberty/regex.c	(working copy)
@@ -685,7 +685,7 @@ 
 #  define EXTRACT_NUMBER(destination, source)				\
   do {									\
     (destination) = *(source) & 0377;					\
-    (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8;		\
+    (destination) += ((unsigned) SIGN_EXTEND_CHAR (*((source) + 1))) << 8; \
   } while (0)
 # endif