Skip to content

Commit

Permalink
Fixed latent SEGVs from malloc() returning NULL.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Jun 19, 2015
1 parent 60cfdad commit 47ac1f8
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions vm/builtin/encoding.cpp
Expand Up @@ -544,6 +544,7 @@ namespace rubinius {

if(len > STACK_BUF_SZ) {
malloc_buf = (uint8_t*)malloc(len);
if(!malloc_buf) rubinius::abort();
buf = malloc_buf;
}

Expand Down
4 changes: 4 additions & 0 deletions vm/builtin/ffi_pointer.cpp
Expand Up @@ -88,6 +88,10 @@ namespace rubinius {
Pointer* Pointer::allocate_memory(STATE, Object* self, Fixnum* size) {
Pointer* obj = state->vm()->new_object<Pointer>(as<Class>(self));
obj->pointer = malloc(size->to_native());;
if(!obj->pointer) {
Exception::memory_error(state);
return NULL;
}
obj->autorelease = false;
obj->set_finalizer = false;
return obj;
Expand Down
4 changes: 4 additions & 0 deletions vm/builtin/io.cpp
Expand Up @@ -661,6 +661,10 @@ namespace rubinius {

if(count > STACK_BUF_SZ) {
malloc_buf = (char*)malloc(count);
if(!malloc_buf) {
Exception::memory_error(state);
return NULL;
}
buf = malloc_buf;
}

Expand Down
5 changes: 5 additions & 0 deletions vm/builtin/string.cpp
Expand Up @@ -1054,6 +1054,11 @@ namespace rubinius {
native_int out_size = out_chunk;
uint8_t* output = (uint8_t*)malloc(out_size);

if(!output) {
Exception::memory_error(state);
return NULL;
}

uint8_t* out_p = output;
uint8_t* out_end = out_p + out_size;

Expand Down
5 changes: 5 additions & 0 deletions vm/builtin/time.cpp
Expand Up @@ -300,6 +300,11 @@ namespace rubinius {
buf_size *= 2;
char* malloc_str = (char*)malloc(buf_size);

if(!malloc_str) {
Exception::memory_error(state);
return NULL;
}

chars = ::strftime_extended(malloc_str, buf_size,
format->c_str(state), &tm, &ts, CBOOL(is_gmt_) ? 1 : 0,
off);
Expand Down
4 changes: 4 additions & 0 deletions vm/capi/string.cpp
Expand Up @@ -461,6 +461,10 @@ extern "C" {
String* str = c_as<String>(env->get_object(self));

char* data = (char*)malloc(sizeof(char) * str->byte_size() + 1);
if(!data) {
rb_raise(rb_eSystemCallError, "unable to allocate memory");
}

memcpy(data, str->c_str(env->state()), str->byte_size());
data[str->byte_size()] = 0;

Expand Down
1 change: 1 addition & 0 deletions vm/fiber_data.cpp
Expand Up @@ -142,6 +142,7 @@ namespace rubinius {

if(heap_) free(heap_);
heap_ = malloc(heap_capacity_);
if(!heap_) rubinius::abort();
}

memcpy(heap_, stack_bottom(), heap_size_);
Expand Down
3 changes: 3 additions & 0 deletions vm/fiber_stack.cpp
Expand Up @@ -28,6 +28,8 @@ namespace rubinius {
void FiberStack::allocate() {
assert(!address_);
address_ = malloc(size_);
if(!address_) rubinius::abort();

#ifdef HAVE_VALGRIND_H
valgrind_id_ = VALGRIND_STACK_REGISTER(address_, (char *)address_ + size_);
#endif
Expand Down Expand Up @@ -150,6 +152,7 @@ namespace rubinius {
void* FiberStacks::trampoline() {
if(trampoline_ == 0) {
trampoline_ = malloc(cTrampolineSize);
if(!trampoline_) rubinius::abort();
}

return trampoline_;
Expand Down
5 changes: 4 additions & 1 deletion vm/gc/mark_sweep.cpp
Expand Up @@ -41,7 +41,10 @@ namespace rubinius {
}

Object* MarkSweepGC::allocate(size_t bytes, bool *collect_now) {
Object* obj = reinterpret_cast<Object*>(malloc(bytes));
void* mem = malloc(bytes);
if(!mem) rubinius::abort();

Object* obj = reinterpret_cast<Object*>(mem);

// If the allocation failed, we return a NULL pointer
if(unlikely(!obj)) {
Expand Down
3 changes: 3 additions & 0 deletions vm/marshal.cpp
Expand Up @@ -44,6 +44,7 @@ namespace rubinius {

if(count >= STACK_BUF_SZ) {
malloc_data = (char*)malloc(count + 1);
if(!malloc_data) rubinius::abort();
data = malloc_data;
}

Expand All @@ -69,6 +70,7 @@ namespace rubinius {

if(count >= STACK_BUF_SZ) {
malloc_data = (char*)malloc(count + 1);
if(!malloc_data) rubinius::abort();
data = malloc_data;
}

Expand Down Expand Up @@ -160,6 +162,7 @@ namespace rubinius {

if(count >= STACK_BUF_SZ) {
malloc_data = (char*)malloc(count + 1);
if(!malloc_data) rubinius::abort();
data = malloc_data;
}

Expand Down
1 change: 1 addition & 0 deletions vm/missing/setproctitle.c
Expand Up @@ -118,6 +118,7 @@ ruby_init_setproctitle(int argc, char *argv[])
size_t len = strlen(envp[i]) + 1;

tmp = malloc(len);
if(!tmp) abort();
memcpy(tmp, envp[i], len);

environ[i] = tmp;
Expand Down
1 change: 1 addition & 0 deletions vm/util/local_buffer.hpp
Expand Up @@ -7,6 +7,7 @@ namespace rubinius {

LocalBuffer(size_t size) {
buffer = malloc(size);
if(!buffer) ::abort();
}

~LocalBuffer() {
Expand Down

0 comments on commit 47ac1f8

Please sign in to comment.