Skip to content

Commit 47ac1f8

Browse files
committedJun 19, 2015
Fixed latent SEGVs from malloc() returning NULL.
1 parent 60cfdad commit 47ac1f8

12 files changed

+36
-1
lines changed
 

‎vm/builtin/encoding.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ namespace rubinius {
544544

545545
if(len > STACK_BUF_SZ) {
546546
malloc_buf = (uint8_t*)malloc(len);
547+
if(!malloc_buf) rubinius::abort();
547548
buf = malloc_buf;
548549
}
549550

‎vm/builtin/ffi_pointer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ namespace rubinius {
8888
Pointer* Pointer::allocate_memory(STATE, Object* self, Fixnum* size) {
8989
Pointer* obj = state->vm()->new_object<Pointer>(as<Class>(self));
9090
obj->pointer = malloc(size->to_native());;
91+
if(!obj->pointer) {
92+
Exception::memory_error(state);
93+
return NULL;
94+
}
9195
obj->autorelease = false;
9296
obj->set_finalizer = false;
9397
return obj;

‎vm/builtin/io.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,10 @@ namespace rubinius {
661661

662662
if(count > STACK_BUF_SZ) {
663663
malloc_buf = (char*)malloc(count);
664+
if(!malloc_buf) {
665+
Exception::memory_error(state);
666+
return NULL;
667+
}
664668
buf = malloc_buf;
665669
}
666670

‎vm/builtin/string.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,11 @@ namespace rubinius {
10541054
native_int out_size = out_chunk;
10551055
uint8_t* output = (uint8_t*)malloc(out_size);
10561056

1057+
if(!output) {
1058+
Exception::memory_error(state);
1059+
return NULL;
1060+
}
1061+
10571062
uint8_t* out_p = output;
10581063
uint8_t* out_end = out_p + out_size;
10591064

‎vm/builtin/time.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ namespace rubinius {
300300
buf_size *= 2;
301301
char* malloc_str = (char*)malloc(buf_size);
302302

303+
if(!malloc_str) {
304+
Exception::memory_error(state);
305+
return NULL;
306+
}
307+
303308
chars = ::strftime_extended(malloc_str, buf_size,
304309
format->c_str(state), &tm, &ts, CBOOL(is_gmt_) ? 1 : 0,
305310
off);

‎vm/capi/string.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@ extern "C" {
461461
String* str = c_as<String>(env->get_object(self));
462462

463463
char* data = (char*)malloc(sizeof(char) * str->byte_size() + 1);
464+
if(!data) {
465+
rb_raise(rb_eSystemCallError, "unable to allocate memory");
466+
}
467+
464468
memcpy(data, str->c_str(env->state()), str->byte_size());
465469
data[str->byte_size()] = 0;
466470

‎vm/fiber_data.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ namespace rubinius {
142142

143143
if(heap_) free(heap_);
144144
heap_ = malloc(heap_capacity_);
145+
if(!heap_) rubinius::abort();
145146
}
146147

147148
memcpy(heap_, stack_bottom(), heap_size_);

‎vm/fiber_stack.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ namespace rubinius {
2828
void FiberStack::allocate() {
2929
assert(!address_);
3030
address_ = malloc(size_);
31+
if(!address_) rubinius::abort();
32+
3133
#ifdef HAVE_VALGRIND_H
3234
valgrind_id_ = VALGRIND_STACK_REGISTER(address_, (char *)address_ + size_);
3335
#endif
@@ -150,6 +152,7 @@ namespace rubinius {
150152
void* FiberStacks::trampoline() {
151153
if(trampoline_ == 0) {
152154
trampoline_ = malloc(cTrampolineSize);
155+
if(!trampoline_) rubinius::abort();
153156
}
154157

155158
return trampoline_;

‎vm/gc/mark_sweep.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ namespace rubinius {
4141
}
4242

4343
Object* MarkSweepGC::allocate(size_t bytes, bool *collect_now) {
44-
Object* obj = reinterpret_cast<Object*>(malloc(bytes));
44+
void* mem = malloc(bytes);
45+
if(!mem) rubinius::abort();
46+
47+
Object* obj = reinterpret_cast<Object*>(mem);
4548

4649
// If the allocation failed, we return a NULL pointer
4750
if(unlikely(!obj)) {

‎vm/marshal.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace rubinius {
4444

4545
if(count >= STACK_BUF_SZ) {
4646
malloc_data = (char*)malloc(count + 1);
47+
if(!malloc_data) rubinius::abort();
4748
data = malloc_data;
4849
}
4950

@@ -69,6 +70,7 @@ namespace rubinius {
6970

7071
if(count >= STACK_BUF_SZ) {
7172
malloc_data = (char*)malloc(count + 1);
73+
if(!malloc_data) rubinius::abort();
7274
data = malloc_data;
7375
}
7476

@@ -160,6 +162,7 @@ namespace rubinius {
160162

161163
if(count >= STACK_BUF_SZ) {
162164
malloc_data = (char*)malloc(count + 1);
165+
if(!malloc_data) rubinius::abort();
163166
data = malloc_data;
164167
}
165168

‎vm/missing/setproctitle.c

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ ruby_init_setproctitle(int argc, char *argv[])
118118
size_t len = strlen(envp[i]) + 1;
119119

120120
tmp = malloc(len);
121+
if(!tmp) abort();
121122
memcpy(tmp, envp[i], len);
122123

123124
environ[i] = tmp;

‎vm/util/local_buffer.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace rubinius {
77

88
LocalBuffer(size_t size) {
99
buffer = malloc(size);
10+
if(!buffer) ::abort();
1011
}
1112

1213
~LocalBuffer() {

0 commit comments

Comments
 (0)
Please sign in to comment.