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 fcea881 commit 19b2cd1
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 2 deletions.
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 @@ -558,6 +558,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 @@ -460,6 +460,10 @@ extern "C" {
String* str = c_as<String>(env->get_object(self));

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

memcpy(data, str->c_str(env->state()), str->size());
data[str->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
2 changes: 2 additions & 0 deletions vm/marshal.cpp
Expand Up @@ -40,6 +40,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 @@ -88,6 +89,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
3 changes: 2 additions & 1 deletion vm/util/strftime.c
Expand Up @@ -231,8 +231,9 @@ strftime_extended(char *s, size_t maxsize, const char *format, const struct tm64
if (savetz) {
strncpy(savetz, tz, i);
}
} else
} else {
strncpy(savetz, tz, i);
}
tzset();
}
#endif /* POSIX_SEMANTICS */
Expand Down

0 comments on commit 19b2cd1

Please sign in to comment.