Error checking for length parameter of external array constructors in shell

BUG=v8:1501

Review URL: http://codereview.chromium.org/7268002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8458 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 61c7b030
......@@ -497,14 +497,39 @@ void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) {
v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args,
v8::ExternalArrayType type,
int element_size) {
size_t element_size) {
ASSERT(element_size == 1 || element_size == 2 || element_size == 4 ||
element_size == 8);
if (args.Length() != 1) {
return v8::ThrowException(
v8::String::New("Array constructor needs one parameter."));
}
int length = args[0]->Int32Value();
void* data = malloc(length * element_size);
memset(data, 0, length * element_size);
size_t length = 0;
if (args[0]->IsUint32()) {
length = args[0]->Uint32Value();
} else if (args[0]->IsNumber()) {
double raw_length = args[0]->NumberValue();
if (raw_length < 0) {
return v8::ThrowException(
v8::String::New("Array length must not be negative."));
}
if (raw_length > v8::internal::ExternalArray::kMaxLength) {
return v8::ThrowException(
v8::String::New("Array length exceeds maximum length."));
}
length = static_cast<size_t>(raw_length);
} else {
return v8::ThrowException(
v8::String::New("Array length must be a number."));
}
if (length > static_cast<size_t>(v8::internal::ExternalArray::kMaxLength)) {
return v8::ThrowException(
v8::String::New("Array length exceeds maximum length."));
}
void* data = calloc(length, element_size);
if (data == NULL) {
return v8::ThrowException(v8::String::New("Memory allocation failed."));
}
v8::Handle<v8::Object> array = v8::Object::New();
v8::Persistent<v8::Object> persistent_array =
v8::Persistent<v8::Object>::New(array);
......
......@@ -223,14 +223,36 @@ Handle<Value> Shell::Load(const Arguments& args) {
Handle<Value> Shell::CreateExternalArray(const Arguments& args,
ExternalArrayType type,
int element_size) {
size_t element_size) {
ASSERT(element_size == 1 || element_size == 2 || element_size == 4 ||
element_size == 8);
if (args.Length() != 1) {
return ThrowException(
String::New("Array constructor needs one parameter."));
}
int length = args[0]->Int32Value();
void* data = malloc(length * element_size);
memset(data, 0, length * element_size);
size_t length = 0;
if (args[0]->IsUint32()) {
length = args[0]->Uint32Value();
} else if (args[0]->IsNumber()) {
double raw_length = args[0]->NumberValue();
if (raw_length < 0) {
return ThrowException(String::New("Array length must not be negative."));
}
if (raw_length > v8::internal::ExternalArray::kMaxLength) {
return ThrowException(
String::New("Array length exceeds maximum length."));
}
length = static_cast<size_t>(raw_length);
} else {
return ThrowException(String::New("Array length must be a number."));
}
if (length > static_cast<size_t>(internal::ExternalArray::kMaxLength)) {
return ThrowException(String::New("Array length exceeds maximum length."));
}
void* data = calloc(length, element_size);
if (data == NULL) {
return ThrowException(String::New("Memory allocation failed."));
}
Handle<Object> array = Object::New();
Persistent<Object> persistent_array = Persistent<Object>::New(array);
persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
......
......@@ -217,7 +217,7 @@ class Shell: public i::AllStatic {
static Counter* GetCounter(const char* name, bool is_histogram);
static Handle<Value> CreateExternalArray(const Arguments& args,
ExternalArrayType type,
int element_size);
size_t element_size);
static void ExternalArrayWeakCallback(Persistent<Value> object, void* data);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment