[前][次][番号順一覧][スレッド一覧]

ruby-changes:35915

From: akr <ko1@a...>
Date: Fri, 17 Oct 2014 17:50:15 +0900 (JST)
Subject: [ruby-changes:35915] akr:r47996 (trunk): * Avoid undefined behaviors found by gcc -fsanitize=undefined.

akr	2014-10-17 17:50:01 +0900 (Fri, 17 Oct 2014)

  New Revision: 47996

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47996

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/include/ruby/ruby.h
    trunk/node.h
    trunk/pack.c
    trunk/regint.h
    trunk/time.c
    trunk/vm_core.h
    trunk/vm_trace.c
Index: time.c
===================================================================
--- time.c	(revision 47995)
+++ time.c	(revision 47996)
@@ -4653,7 +4653,7 @@ time_mdump(VALUE time) https://github.com/ruby/ruby/blob/trunk/time.c#L4653
 	(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) https://github.com/ruby/ruby/blob/trunk/time.c#L4766
 
     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) {
Index: include/ruby/ruby.h
===================================================================
--- include/ruby/ruby.h	(revision 47995)
+++ include/ruby/ruby.h	(revision 47996)
@@ -230,7 +230,7 @@ typedef char ruby_check_sizeof_voidp[SIZ https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L230
 #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);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47995)
+++ ChangeLog	(revision 47996)
@@ -1,3 +1,25 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Oct 17 17:43:50 2014  Tanaka Akira  <akr@f...>
+
+	* 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  <nobu@r...>
 
 	* re.c (unescape_nonascii): make dynamically compiled US-ASCII
Index: vm_core.h
===================================================================
--- vm_core.h	(revision 47995)
+++ vm_core.h	(revision 47996)
@@ -811,7 +811,7 @@ enum vm_special_object_type { https://github.com/ruby/ruby/blob/trunk/vm_core.h#L811
 #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<<VM_FRAME_MAGIC_MASK_BITS))
+#define VM_FRAME_MAGIC_MASK   (~(~(VALUE)0<<VM_FRAME_MAGIC_MASK_BITS))
 
 #define VM_FRAME_TYPE(cfp) ((cfp)->flag & VM_FRAME_MAGIC_MASK)
 
Index: pack.c
===================================================================
--- pack.c	(revision 47995)
+++ pack.c	(revision 47996)
@@ -943,13 +943,14 @@ static const char b64_table[] = https://github.com/ruby/ruby/blob/trunk/pack.c#L943
 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
 static void
-encodes(VALUE str, const char *s, long len, int type, int tail_lf)
+encodes(VALUE str, const char *s0, long len, int type, int tail_lf)
 {
     enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
     char buff[buff_size + 1];	/* +1 for tail_lf */
     long i = 0;
     const char *const trans = type == 'u' ? uu_table : b64_table;
     char padding;
+    const unsigned char *s = (const unsigned char *)s0;
 
     if (type == 'u') {
 	buff[i++] = (char)len + ' ';
@@ -1362,7 +1363,7 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1363
 		t = RSTRING_PTR(bitstr);
 		for (i=0; i<len; i++) {
 		    if (i & 7) bits <<= 1;
-		    else bits = *s++;
+		    else bits = (unsigned char)*s++;
 		    *t++ = (bits & 128) ? '1' : '0';
 		}
 	    }
@@ -1406,7 +1407,7 @@ pack_unpack(VALUE str, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L1407
 		    if (i & 1)
 			bits <<= 4;
 		    else
-			bits = *s++;
+			bits = (unsigned char)*s++;
 		    *t++ = hexdigits[(bits >> 4) & 15];
 		}
 	    }
Index: regint.h
===================================================================
--- regint.h	(revision 47995)
+++ regint.h	(revision 47996)
@@ -392,7 +392,7 @@ typedef unsigned int  BitStatusType; https://github.com/ruby/ruby/blob/trunk/regint.h#L392
 #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; https://github.com/ruby/ruby/blob/trunk/regint.h#L468
 } 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)
Index: vm_trace.c
===================================================================
--- vm_trace.c	(revision 47995)
+++ vm_trace.c	(revision 47996)
@@ -67,7 +67,7 @@ recalc_add_ruby_vm_event_flags(rb_event_ https://github.com/ruby/ruby/blob/trunk/vm_trace.c#L67
     ruby_vm_event_flags = 0;
 
     for (i=0; i<MAX_EVENT_NUM; i++) {
-	if (events & (1 << i)) {
+	if (events & ((rb_event_flag_t)1 << i)) {
 	    ruby_event_flag_count[i]++;
 	}
 	ruby_vm_event_flags |= ruby_event_flag_count[i] ? (1<<i) : 0;
Index: node.h
===================================================================
--- node.h	(revision 47995)
+++ node.h	(revision 47996)
@@ -287,7 +287,7 @@ typedef struct RNode { https://github.com/ruby/ruby/blob/trunk/node.h#L287
 #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<<NODE_LSHIFT))|(((l)&NODE_LMASK)<<NODE_LSHIFT))
+    RNODE(n)->flags=((RNODE(n)->flags&~((VALUE)(-1)<<NODE_LSHIFT))|((VALUE)((l)&NODE_LMASK)<<NODE_LSHIFT))
 
 #define nd_refinements  nd_reserved
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]