OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 if (!ExecuteString(source, String::New(*file), false, false)) { | 216 if (!ExecuteString(source, String::New(*file), false, false)) { |
217 return ThrowException(String::New("Error executing file")); | 217 return ThrowException(String::New("Error executing file")); |
218 } | 218 } |
219 } | 219 } |
220 return Undefined(); | 220 return Undefined(); |
221 } | 221 } |
222 | 222 |
223 | 223 |
224 Handle<Value> Shell::CreateExternalArray(const Arguments& args, | 224 Handle<Value> Shell::CreateExternalArray(const Arguments& args, |
225 ExternalArrayType type, | 225 ExternalArrayType type, |
226 int element_size) { | 226 size_t element_size) { |
227 if (args.Length() != 1) { | 227 if (args.Length() != 1) { |
228 return ThrowException( | 228 return ThrowException( |
229 String::New("Array constructor needs one parameter.")); | 229 String::New("Array constructor needs one parameter.")); |
230 } | 230 } |
231 int length = args[0]->Int32Value(); | 231 if (args[0]->Int32Value() < 0) { |
232 void* data = malloc(length * element_size); | 232 return ThrowException(String::New("Array length must not be negative.")); |
233 memset(data, 0, length * element_size); | 233 } |
| 234 size_t length = static_cast<size_t>(args[0]->Int32Value()); |
| 235 if (length > static_cast<size_t>(internal::ExternalArray::kMaxLength)) { |
| 236 return ThrowException(String::New("Array length exceeds maximum length.")); |
| 237 } |
| 238 size_t malloc_size = length * element_size; |
| 239 // Check for overflow in the multiplication. |
| 240 if (malloc_size < length || malloc_size < element_size) { |
| 241 return ThrowException(String::New("Array size exceeds memory limit.")); |
| 242 } |
| 243 void* data = malloc(malloc_size); |
| 244 if (data == NULL) { |
| 245 return ThrowException(String::New("Memory allocation failed.")); |
| 246 } |
| 247 memset(data, 0, malloc_size); |
234 Handle<Object> array = Object::New(); | 248 Handle<Object> array = Object::New(); |
235 Persistent<Object> persistent_array = Persistent<Object>::New(array); | 249 Persistent<Object> persistent_array = Persistent<Object>::New(array); |
236 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); | 250 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); |
237 persistent_array.MarkIndependent(); | 251 persistent_array.MarkIndependent(); |
238 array->SetIndexedPropertiesToExternalArrayData(data, type, length); | 252 array->SetIndexedPropertiesToExternalArrayData(data, type, length); |
239 array->Set(String::New("length"), Int32::New(length), ReadOnly); | 253 array->Set(String::New("length"), Int32::New(length), ReadOnly); |
240 array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size)); | 254 array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size)); |
241 return array; | 255 return array; |
242 } | 256 } |
243 | 257 |
(...skipping 736 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 } | 994 } |
981 | 995 |
982 } // namespace v8 | 996 } // namespace v8 |
983 | 997 |
984 | 998 |
985 #ifndef GOOGLE3 | 999 #ifndef GOOGLE3 |
986 int main(int argc, char* argv[]) { | 1000 int main(int argc, char* argv[]) { |
987 return v8::Shell::Main(argc, argv); | 1001 return v8::Shell::Main(argc, argv); |
988 } | 1002 } |
989 #endif | 1003 #endif |
OLD | NEW |