Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(732)

Unified Diff: samples/shell.cc

Issue 7268002: Error checking for length parameter of external array constructors in shell (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/d8.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/shell.cc
diff --git a/samples/shell.cc b/samples/shell.cc
index 950370adaa80fda9bd7896a41a0c3f32e7e69eaa..72a00f7d7841d7e0ac5eb3dac46d0963a391778e 100644
--- a/samples/shell.cc
+++ b/samples/shell.cc
@@ -497,14 +497,31 @@ 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) {
Lasse Reichstein 2011/06/28 09:28:58 Let's ASSERT that element_size it's a meaningful v
Jakob Kummerow 2011/06/28 14:02:38 Done. Changed the interface to size_t anyway becau
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);
+ if (args[0]->Int32Value() < 0) {
Lasse Reichstein 2011/06/28 09:28:58 You convert args[0] to int32 twice. Just do it onc
Lasse Reichstein 2011/06/28 09:44:12 That is, ofcourse, unless there is a specification
Jakob Kummerow 2011/06/28 14:02:38 Spec says the c'tor argument is an "unsigned long"
Lasse Reichstein 2011/06/29 08:51:05 An unsigned long can take values that are too big
+ return v8::ThrowException(
+ v8::String::New("Array length must not be negative."));
+ }
+ size_t length = static_cast<size_t>(args[0]->Int32Value());
+ if (length > static_cast<size_t>(v8::internal::ExternalArray::kMaxLength)) {
Lasse Reichstein 2011/06/28 09:28:58 If both values are int32, there's no need to conve
Jakob Kummerow 2011/06/28 14:02:38 True. Removed the cast.
+ return v8::ThrowException(
+ v8::String::New("Array length exceeds maximum length."));
+ }
+ size_t malloc_size = length * element_size;
+ // Check for overflow in the multiplication.
+ if (malloc_size / length != element_size) {
+ return v8::ThrowException(
+ v8::String::New("Array size exceeds memory limit."));
Lasse Reichstein 2011/06/28 09:28:58 Can this happen? I.e., is kMaxLength * maximal ele
Jakob Kummerow 2011/06/28 14:02:38 It can happen. kMaxLength is 2^30 - 1, and Float64
+ }
+ void* data = malloc(malloc_size);
Lasse Reichstein 2011/06/28 09:28:58 How about using calloc instead? It seems like just
Jakob Kummerow 2011/06/28 14:02:38 Done, thanks for the hint.
+ if (data == NULL) {
+ return v8::ThrowException(v8::String::New("Memory allocation failed."));
+ }
+ memset(data, 0, malloc_size);
v8::Handle<v8::Object> array = v8::Object::New();
v8::Persistent<v8::Object> persistent_array =
v8::Persistent<v8::Object>::New(array);
« no previous file with comments | « no previous file | src/d8.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698