Skip to content

Commit

Permalink
Use stricter type checks to prevent potential null pointer dereferences
Browse files Browse the repository at this point in the history
  • Loading branch information
dbussink committed Apr 19, 2014
1 parent 70e5eeb commit 12a9c81
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions vm/builtin/regexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,17 +627,16 @@ namespace rubinius {
}

String* MatchData::matched_string(STATE) {
Fixnum* beg = try_as<Fixnum>(full_->at(state, 0));
Fixnum* fin = try_as<Fixnum>(full_->at(state, 1));
Fixnum* beg = as<Fixnum>(full_->at(state, 0));
Fixnum* fin = as<Fixnum>(full_->at(state, 1));

native_int max = source_->byte_size();
native_int f = fin->to_native();
native_int b = beg->to_native();

String* string;

if(!beg || !fin ||
f > max || b > max || b < 0) {
if(f > max || b > max || b < 0) {
string = String::create(state, 0, 0);
} else {
const char* str = (char*)source_->byte_address();
Expand All @@ -649,19 +648,19 @@ namespace rubinius {
source_->infect(state, string);
string->encoding_from(state, source_);
string->klass(state, source_->class_object(state));

return string;
}

String* MatchData::pre_matched(STATE) {
Fixnum* beg = try_as<Fixnum>(full_->at(state, 0));
Fixnum* beg = as<Fixnum>(full_->at(state, 0));

native_int max = source_->byte_size();
native_int sz = beg->to_native();

String* string;

if(!beg || sz <= 0) {
if(sz <= 0) {
string = String::create(state, 0, 0);
} else {
if(sz > max) sz = max;
Expand All @@ -679,14 +678,14 @@ namespace rubinius {
}

String* MatchData::post_matched(STATE) {
Fixnum* fin = try_as<Fixnum>(full_->at(state, 1));
Fixnum* fin = as<Fixnum>(full_->at(state, 1));

native_int f = fin->to_native();
native_int max = source_->byte_size();

String* string;

if(!fin || f >= max) {
if(f >= max) {
string = String::create(state, 0, 0);
} else {
const char* str = (char*)source_->byte_address();
Expand All @@ -710,16 +709,15 @@ namespace rubinius {
Tuple* sub = try_as<Tuple>(region_->at(state, which));
if(!sub) return nil<String>();

Fixnum* beg = try_as<Fixnum>(sub->at(state, 0));
Fixnum* fin = try_as<Fixnum>(sub->at(state, 1));
Fixnum* beg = as<Fixnum>(sub->at(state, 0));
Fixnum* fin = as<Fixnum>(sub->at(state, 1));

native_int b = beg->to_native();
native_int f = fin->to_native();
native_int max = source_->byte_size();

if(!beg || !fin ||
f > max ||
b < 0) {
if(f > max ||
b < 0) {
return nil<String>();
}

Expand Down

0 comments on commit 12a9c81

Please sign in to comment.