Bug 1227207 - throw correct error type. r=jorendorff

--HG--
extra : rebase_source : 1df2065fc504e57f67585e42a6e8925fbcdbeb25
This commit is contained in:
Lars T Hansen 2016-05-18 11:51:33 +02:00
Родитель dacd6df3ac
Коммит f63085b198
5 изменённых файлов: 22 добавлений и 8 удалений

Просмотреть файл

@ -493,6 +493,7 @@ MSG_DEF(JSMSG_SHORT_ARRAY_BUFFER_RETURNED, 2, JSEXN_TYPEERR, "expected ArrayBuff
MSG_DEF(JSMSG_TYPED_ARRAY_BAD_ARGS, 0, JSEXN_TYPEERR, "invalid arguments")
MSG_DEF(JSMSG_TYPED_ARRAY_NEGATIVE_ARG,1, JSEXN_RANGEERR, "argument {0} must be >= 0")
MSG_DEF(JSMSG_TYPED_ARRAY_DETACHED, 0, JSEXN_TYPEERR, "attempting to access detached ArrayBuffer")
MSG_DEF(JSMSG_TYPED_ARRAY_CONSTRUCT_BOUNDS, 0, JSEXN_RANGEERR, "attempting to construct out-of-bounds TypedArray on ArrayBuffer")
// Shared array buffer
MSG_DEF(JSMSG_SHARED_ARRAY_BAD_LENGTH, 0, JSEXN_RANGEERR, "length argument out of range")

Просмотреть файл

@ -0,0 +1,13 @@
// Bug 1227207
var AB = new ArrayBuffer(12); // Length divides 4
var BC = new ArrayBuffer(14); // Length does not divide 4
assertThrowsInstanceOf(() => new Int32Array(AB, -1), RangeError); // 22.2.4.5 #8
assertThrowsInstanceOf(() => new Int32Array(AB, 2), RangeError); // 22.2.4.5 #10
assertThrowsInstanceOf(() => new Int32Array(BC), RangeError); // 22.2.4.5 #13.a
assertThrowsInstanceOf(() => new Int32Array(AB, 16), RangeError); // 22.2.4.5 #13.c
assertThrowsInstanceOf(() => new Int32Array(AB, 0, 4), RangeError); // 22.2.4.5 #14.c
if (typeof reportCompare === "function")
reportCompare(true, true);

Просмотреть файл

@ -82,13 +82,13 @@ function testSharedTypedArray() {
assertThrowsInstanceOf(() => new Int8Array(b, -7), RangeError);
// not congruent mod element size
assertThrowsInstanceOf(() => new Int32Array(b, 3), TypeError); // Bug 1227207: should be RangeError
assertThrowsInstanceOf(() => new Int32Array(b, 3), RangeError);
// start out of range
assertThrowsInstanceOf(() => new Int32Array(b, 4104), TypeError); // Ditto
assertThrowsInstanceOf(() => new Int32Array(b, 4104), RangeError);
// end out of range
assertThrowsInstanceOf(() => new Int32Array(b, 4092, 2), TypeError); // Ditto
assertThrowsInstanceOf(() => new Int32Array(b, 4092, 2), RangeError);
// Views alias the storage
x2[0] = -1;

Просмотреть файл

@ -1172,7 +1172,7 @@ intrinsic_MoveTypedArrayElements(JSContext* cx, unsigned argc, Value* vp)
"the not-detached requirement is wrong");
if (tarray->hasDetachedBuffer()) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_BAD_ARGS);
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_DETACHED);
return false;
}

Просмотреть файл

@ -605,7 +605,7 @@ class TypedArrayObjectTemplate : public TypedArrayObject
}
if (byteOffset > buffer->byteLength() || byteOffset % sizeof(NativeType) != 0) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_BAD_ARGS);
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_CONSTRUCT_BOUNDS);
return nullptr; // invalid byteOffset
}
@ -614,7 +614,7 @@ class TypedArrayObjectTemplate : public TypedArrayObject
len = (buffer->byteLength() - byteOffset) / sizeof(NativeType);
if (len * sizeof(NativeType) != buffer->byteLength() - byteOffset) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr,
JSMSG_TYPED_ARRAY_BAD_ARGS);
JSMSG_TYPED_ARRAY_CONSTRUCT_BOUNDS);
return nullptr; // given byte array doesn't map exactly to sizeof(NativeType) * N
}
} else {
@ -624,12 +624,12 @@ class TypedArrayObjectTemplate : public TypedArrayObject
// Go slowly and check for overflow.
uint32_t arrayByteLength = len * sizeof(NativeType);
if (len >= INT32_MAX / sizeof(NativeType) || byteOffset >= INT32_MAX - arrayByteLength) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_BAD_ARGS);
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_CONSTRUCT_BOUNDS);
return nullptr; // overflow when calculating byteOffset + len * sizeof(NativeType)
}
if (arrayByteLength + byteOffset > buffer->byteLength()) {
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_BAD_ARGS);
JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_TYPED_ARRAY_CONSTRUCT_BOUNDS);
return nullptr; // byteOffset + len is too big for the arraybuffer
}