This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]