Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 19b2cd1

Browse files
committedJun 19, 2015
Fixed latent SEGVs from malloc() returning NULL.
1 parent fcea881 commit 19b2cd1

12 files changed

+36
-2
lines changed
 

‎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
@@ -558,6 +558,11 @@ namespace rubinius {
558558
native_int out_size = out_chunk;
559559
uint8_t* output = (uint8_t*)malloc(out_size);
560560

561+
if(!output) {
562+
Exception::memory_error(state);
563+
return NULL;
564+
}
565+
561566
uint8_t* out_p = output;
562567
uint8_t* out_end = out_p + out_size;
563568

‎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
@@ -460,6 +460,10 @@ extern "C" {
460460
String* str = c_as<String>(env->get_object(self));
461461

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

‎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

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace rubinius {
4040

4141
if(count >= STACK_BUF_SZ) {
4242
malloc_data = (char*)malloc(count + 1);
43+
if(!malloc_data) rubinius::abort();
4344
data = malloc_data;
4445
}
4546

@@ -88,6 +89,7 @@ namespace rubinius {
8889

8990
if(count >= STACK_BUF_SZ) {
9091
malloc_data = (char*)malloc(count + 1);
92+
if(!malloc_data) rubinius::abort();
9193
data = malloc_data;
9294
}
9395

‎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() {

‎vm/util/strftime.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ strftime_extended(char *s, size_t maxsize, const char *format, const struct tm64
231231
if (savetz) {
232232
strncpy(savetz, tz, i);
233233
}
234-
} else
234+
} else {
235235
strncpy(savetz, tz, i);
236+
}
236237
tzset();
237238
}
238239
#endif /* POSIX_SEMANTICS */

0 commit comments

Comments
 (0)
Please sign in to comment.