Skip to content

Commit e3a2ffc

Browse files
committed
Guard against malloc of size zero, which has implementation-defined behaviour
1 parent 9757710 commit e3a2ffc

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/main/c/cext/ruby.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ void *ruby_xmalloc(size_t size) {
7272
}
7373

7474
void *ruby_xmalloc2(size_t n, size_t size) {
75-
return malloc(xmalloc2_size(n, size));
75+
size_t total_size = xmalloc2_size(n, size);
76+
if (total_size == 0) {
77+
total_size = 1;
78+
}
79+
return malloc(xmalloc2_size(n, total_size));
7680
}
7781

7882
void *ruby_xcalloc(size_t n, size_t size) {
@@ -96,6 +100,9 @@ void ruby_xfree(void *address) {
96100
}
97101

98102
void *rb_alloc_tmp_buffer(volatile VALUE *store, long len) {
103+
if (len == 0) {
104+
len = 1;
105+
}
99106
void *ptr = malloc(len);
100107
*((void**)store) = ptr;
101108
return ptr;

0 commit comments

Comments
 (0)