8000 * Avoid undefined behaviors found by gcc -fsanitize=undefined. · ruby/ruby@1efb3c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1efb3c3

Browse files
committed
* 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
1 parent 0f67a3b commit 1efb3c3

File tree

8 files changed

+35
-12
lines changed

8 files changed

+35
-12
lines changed

ChangeLog

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
Fri Oct 17 17:43:50 2014 Tanaka Akira <akr@fsij.org>
2+
3+
* Avoid undefined behaviors found by gcc -fsanitize=undefined.
4+
gcc (Debian 4.9.1-16) 4.9.1
5+
6+
* include/ruby/ruby.h (INT2FIX): Avoid undefined behavior.
7+
8+
* node.h (nd_set_line): Ditto.
9+
10+
* pack.c (encodes): Ditto.
11+
(pack_unpack): Ditto.
12+
13+
* regint.h (BIT_STATUS_AT): Ditto.
14+
(BS_BIT): Ditto.
15+
16+
* time.c (time_mdump): Ditto.
17+
(time_mload): Ditto.
18+
19+
* vm_core.h (VM_FRAME_MAGIC_MASK): Ditto.
20+
21+
* vm_trace.c (recalc_add_ruby_vm_event_flags): Ditto.
22+
123
Fri Oct 17 15:06:49 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
224

325
* re.c (unescape_nonascii): make dynamically compiled US-ASCII

include/ruby/ruby.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ typedef char ruby_check_sizeof_voidp[SIZEOF_VOIDP == sizeof(void*) ? 1 : -1];
230230
#define FIXNUM_MAX (LONG_MAX>>1)
231231
#define FIXNUM_MIN RSHIFT((long)LONG_MIN,1)
232232

233-
#define INT2FIX(i) ((VALUE)(((SIGNED_VALUE)(i))<<1 | FIXNUM_FLAG))
233+
#define INT2FIX(i) (((VALUE)(i))<<1 | FIXNUM_FLAG)
234234
#define LONG2FIX(i) INT2FIX(i)
235235
#define rb_fix_new(v) INT2FIX(v)
236236
VALUE rb_int2inum(SIGNED_VALUE);

node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ typedef struct RNode {
287287
#define NODE_LMASK (((SIGNED_VALUE)1<<(sizeof(VALUE)*CHAR_BIT-NODE_LSHIFT))-1)
288288
#define nd_line(n) (int)(RNODE(n)->flags>>NODE_LSHIFT)
289289
#define nd_set_line(n,l) \
290-
RNODE(n)->flags=((RNODE(n)->flags&~(-1<<NODE_LSHIFT))|(((l)&NODE_LMASK)<<NODE_LSHIFT))
290+
RNODE(n)->flags=((RNODE(n)->flags&~((VALUE)(-1)<<NODE_LSHIFT))|((VALUE)((l)&NODE_LMASK)<<NODE_LSHIFT))
291291

292292
#define nd_refinements nd_reserved
293293

pack.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,13 +943,14 @@ static const char b64_table[] =
943943
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
944944

945945
static void
946-
encodes(VALUE str, const char *s, long len, int type, int tail_lf)
946+
encodes(VALUE str, const char *s0, long len, int type, int tail_lf)
947947
{
948948
enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
949949
char buff[buff_size + 1]; /* +1 for tail_lf */
950950
long i = 0;
951951
const char *const trans = type == 'u' ? uu_table : b64_table;
952952
char padding;
953+
const unsigned char *s = (const unsigned char *)s0;
953954

954955
if (type == 'u') {
955956
buff[i++] = (char)len + ' ';
@@ -1362,7 +1363,7 @@ pack_unpack(VALUE str, VALUE fmt)
13621363
t = RSTRING_PTR(bitstr);
13631364
for (i=0; i<len; i++) {
13641365
if (i & 7) bits <<= 1;
1365-
else bits = *s++;
1366+
else bits = (unsigned char)*s++;
13661367
*t++ = (bits & 128) ? '1' : '0';
13671368
}
13681369
}
@@ -1406,7 +1407,7 @@ pack_unpack(VALUE str, VALUE fmt)
14061407
if (i & 1)
14071408
bits <<= 4;
14081409
else
1409-
bits = *s++;
1410+
bits = (unsigned char)*s++;
14101411
*t++ = hexdigits[(bits >> 4) & 15];
14111412
}
14121413
}

regint.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ typedef unsigned int BitStatusType;
392392
#define BIT_STATUS_CLEAR(stats) (stats) = 0
393393
#define BIT_STATUS_ON_ALL(stats) (stats) = ~((BitStatusType )0)
394394
#define BIT_STATUS_AT(stats,n) \
395-
((n) < (int )BIT_STATUS_BITS_NUM ? ((stats) & (1 << n)) : ((stats) & 1))
395+
((n) < (int )BIT_STATUS_BITS_NUM ? ((stats) & ((BitStatusType)1 << n)) : ((stats) & 1))
396396

397397
#define BIT_STATUS_ON_AT(stats,n) do {\
398398
if ((n) < (int )BIT_STATUS_BITS_NUM) \
@@ -468,7 +468,7 @@ typedef Bits* BitSetRef;
468468
} while (0)
469469

470470
#define BS_ROOM(bs,pos) (bs)[(int )(pos) / BITS_IN_ROOM]
471-
#define BS_BIT(pos) (1 << ((int )(pos) % BITS_IN_ROOM))
471+
#define BS_BIT(pos) (1U << ((int )(pos) % BITS_IN_ROOM))
472472

473473
#define BITSET_AT(bs, pos) (BS_ROOM(bs,pos) & BS_BIT(pos))
474474
#define BITSET_SET_BIT(bs, pos) BS_ROOM(bs,pos) |= BS_BIT(pos)

time.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4653,7 +4653,7 @@ time_mdump(VALUE time)
46534653
(vtm.mon-1) << 10 | /* 4 */
46544654
vtm.mday << 5 | /* 5 */
46554655
vtm.hour; /* 5 */
4656-
s = vtm.min << 26 | /* 6 */
4656+
s = (unsigned long)vtm.min << 26 | /* 6 */
46574657
vtm.sec << 20 | /* 6 */
46584658
usec; /* 20 */
46594659

@@ -4766,10 +4766,10 @@ time_mload(VALUE time, VALUE str)
47664766

47674767
p = s = 0;
47684768
for (i=0; i<4; i++) {
4769-
p |= buf[i]<<(8*i);
4769+
p |= (unsigned long)buf[i]<<(8*i);
47704770
}
47714771
for (i=4; i<8; i++) {
4772-
s |= buf[i]<<(8*(i-4));
4772+
s |= (unsigned long)buf[i]<<(8*(i-4));
47734773
}
47744774

47754775
if ((p & (1UL<<31)) == 0) {

vm_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ enum vm_special_object_type {
811811
#define VM_FRAME_MAGIC_LAMBDA 0xa1
812812
#define VM_FRAME_MAGIC_RESCUE 0xb1
813813
#define VM_FRAME_MAGIC_MASK_BITS 8
814-
#define VM_FRAME_MAGIC_MASK (~(~0<<VM_FRAME_MAGIC_MASK_BITS))
814+
#define VM_FRAME_MAGIC_MASK (~(~(VALUE)0<<VM_FRAME_MAGIC_MASK_BITS))
815815

816816
#define VM_FRAME_TYPE(cfp) ((cfp)->flag & VM_FRAME_MAGIC_MASK)
817817

vm_trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ recalc_add_ruby_vm_event_flags(rb_event_flag_t events)
6767
ruby_vm_event_flags = 0;
6868

6969
for (i=0; i<MAX_EVENT_NUM; i++) {
70-
if (events & (1 << i)) {
70+
if (events & ((rb_event_flag_t)1 << i)) {
7171
ruby_event_flag_count[i]++;
7272
}
7373
ruby_vm_event_flags |= ruby_event_flag_count[i] ? (1<<i) : 0;

0 commit comments

Comments
 (0)
0