From 1efb3c31b731e99627bbc0da13dfd3463bb67c67 Mon Sep 17 00:00:00 2001 From: akr Date: Fri, 17 Oct 2014 08:50:01 +0000 Subject: * Avoid undefined behaviors found by gcc -fsanitize=undefined. gcc (Debian 4.9.1-16) 4.9.1 * include/ruby/ruby.h (INT2FIX): Avoid undefined behavior. * node.h (nd_set_line): Ditto. * pack.c (encodes): Ditto. (pack_unpack): Ditto. * regint.h (BIT_STATUS_AT): Ditto. (BS_BIT): Ditto. * time.c (time_mdump): Ditto. (time_mload): Ditto. * vm_core.h (VM_FRAME_MAGIC_MASK): Ditto. * vm_trace.c (recalc_add_ruby_vm_event_flags): Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47996 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 22 ++++++++++++++++++++++ include/ruby/ruby.h | 2 +- node.h | 2 +- pack.c | 7 ++++--- regint.h | 4 ++-- time.c | 6 +++--- vm_core.h | 2 +- vm_trace.c | 2 +- 8 files changed, 35 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5283c86d49..b5dd1f2ea8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,25 @@ +Fri Oct 17 17:43:50 2014 Tanaka Akira + + * Avoid undefined behaviors found by gcc -fsanitize=undefined. + gcc (Debian 4.9.1-16) 4.9.1 + + * include/ruby/ruby.h (INT2FIX): Avoid undefined behavior. + + * node.h (nd_set_line): Ditto. + + * pack.c (encodes): Ditto. + (pack_unpack): Ditto. + + * regint.h (BIT_STATUS_AT): Ditto. + (BS_BIT): Ditto. + + * time.c (time_mdump): Ditto. + (time_mload): Ditto. + + * vm_core.h (VM_FRAME_MAGIC_MASK): Ditto. + + * vm_trace.c (recalc_add_ruby_vm_event_flags): Ditto. + Fri Oct 17 15:06:49 2014 Nobuyoshi Nakada * re.c (unescape_nonascii): make dynamically compiled US-ASCII diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 539c1b3f63..8b133cd7cc 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -230,7 +230,7 @@ typedef char ruby_check_sizeof_voidp[SIZEOF_VOIDP == sizeof(void*) ? 1 : -1]; #define FIXNUM_MAX (LONG_MAX>>1) #define FIXNUM_MIN RSHIFT((long)LONG_MIN,1) -#define INT2FIX(i) ((VALUE)(((SIGNED_VALUE)(i))<<1 | FIXNUM_FLAG)) +#define INT2FIX(i) (((VALUE)(i))<<1 | FIXNUM_FLAG) #define LONG2FIX(i) INT2FIX(i) #define rb_fix_new(v) INT2FIX(v) VALUE rb_int2inum(SIGNED_VALUE); diff --git a/node.h b/node.h index 5055af438f..e376e57bbb 100644 --- a/node.h +++ b/node.h @@ -287,7 +287,7 @@ typedef struct RNode { #define NODE_LMASK (((SIGNED_VALUE)1<<(sizeof(VALUE)*CHAR_BIT-NODE_LSHIFT))-1) #define nd_line(n) (int)(RNODE(n)->flags>>NODE_LSHIFT) #define nd_set_line(n,l) \ - RNODE(n)->flags=((RNODE(n)->flags&~(-1<flags=((RNODE(n)->flags&~((VALUE)(-1)<> 4) & 15]; } } diff --git a/regint.h b/regint.h index 3abc8809c9..855bedd7f9 100644 --- a/regint.h +++ b/regint.h @@ -392,7 +392,7 @@ typedef unsigned int BitStatusType; #define BIT_STATUS_CLEAR(stats) (stats) = 0 #define BIT_STATUS_ON_ALL(stats) (stats) = ~((BitStatusType )0) #define BIT_STATUS_AT(stats,n) \ - ((n) < (int )BIT_STATUS_BITS_NUM ? ((stats) & (1 << n)) : ((stats) & 1)) + ((n) < (int )BIT_STATUS_BITS_NUM ? ((stats) & ((BitStatusType)1 << n)) : ((stats) & 1)) #define BIT_STATUS_ON_AT(stats,n) do {\ if ((n) < (int )BIT_STATUS_BITS_NUM) \ @@ -468,7 +468,7 @@ typedef Bits* BitSetRef; } while (0) #define BS_ROOM(bs,pos) (bs)[(int )(pos) / BITS_IN_ROOM] -#define BS_BIT(pos) (1 << ((int )(pos) % BITS_IN_ROOM)) +#define BS_BIT(pos) (1U << ((int )(pos) % BITS_IN_ROOM)) #define BITSET_AT(bs, pos) (BS_ROOM(bs,pos) & BS_BIT(pos)) #define BITSET_SET_BIT(bs, pos) BS_ROOM(bs,pos) |= BS_BIT(pos) diff --git a/time.c b/time.c index 06bcd11382..53b3cde96d 100644 --- a/time.c +++ b/time.c @@ -4653,7 +4653,7 @@ time_mdump(VALUE time) (vtm.mon-1) << 10 | /* 4 */ vtm.mday << 5 | /* 5 */ vtm.hour; /* 5 */ - s = vtm.min << 26 | /* 6 */ + s = (unsigned long)vtm.min << 26 | /* 6 */ vtm.sec << 20 | /* 6 */ usec; /* 20 */ @@ -4766,10 +4766,10 @@ time_mload(VALUE time, VALUE str) p = s = 0; for (i=0; i<4; i++) { - p |= buf[i]<<(8*i); + p |= (unsigned long)buf[i]<<(8*i); } for (i=4; i<8; i++) { - s |= buf[i]<<(8*(i-4)); + s |= (unsigned long)buf[i]<<(8*(i-4)); } if ((p & (1UL<<31)) == 0) { diff --git a/vm_core.h b/vm_core.h index d4db5d9085..9f0f053960 100644 --- a/vm_core.h +++ b/vm_core.h @@ -811,7 +811,7 @@ enum vm_special_object_type { #define VM_FRAME_MAGIC_LAMBDA 0xa1 #define VM_FRAME_MAGIC_RESCUE 0xb1 #define VM_FRAME_MAGIC_MASK_BITS 8 -#define VM_FRAME_MAGIC_MASK (~(~0<flag & VM_FRAME_MAGIC_MASK) diff --git a/vm_trace.c b/vm_trace.c index c410be7138..6bdc961ed5 100644 --- a/vm_trace.c +++ b/vm_trace.c @@ -67,7 +67,7 @@ recalc_add_ruby_vm_event_flags(rb_event_flag_t events) ruby_vm_event_flags = 0; for (i=0; i