Commit 1dd803f9 authored by vabr's avatar vabr Committed by Commit bot

Fix error message for invalid buffer offset

The constructor for TypedArray in js/typedarray.js emitted
kInvalidTypedArrayAlignment if the array offset exceeded the size of the
underlying buffer. This seems like a typo introduced in
https://codereview.chromium.org/2090353003.

The error message to be emitted instead coincides with the already existing
kInvalidDataViewOffset. The message string is independent of whether the
object in question is a DataView or a typed array, so this CL:
  (1) renames kInvalidDataViewOffset to just kInvalidOffset, and
  (2) uses kInvalidOffset instead of kInvalidTypedArrayAlignment for cases
      when the TypedArray is constructed with an offset exceeding the buffer
      size.

BUG=v8:5733
TEST=Run d8, execute "new Uint8Array(new ArrayBuffer(1),2)", see the error message mention the invalid offset 2.

Review-Url: https://codereview.chromium.org/2692753002
Cr-Commit-Position: refs/heads/master@{#43151}
parent e08f85fc
...@@ -47,8 +47,7 @@ BUILTIN(DataViewConstructor_ConstructStub) { ...@@ -47,8 +47,7 @@ BUILTIN(DataViewConstructor_ConstructStub) {
Handle<Object> offset; Handle<Object> offset;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION( ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, offset, isolate, offset,
Object::ToIndex(isolate, byte_offset, Object::ToIndex(isolate, byte_offset, MessageTemplate::kInvalidOffset));
MessageTemplate::kInvalidDataViewOffset));
// 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
// We currently violate the specification at this point. // We currently violate the specification at this point.
...@@ -60,8 +59,7 @@ BUILTIN(DataViewConstructor_ConstructStub) { ...@@ -60,8 +59,7 @@ BUILTIN(DataViewConstructor_ConstructStub) {
// 7. If offset > bufferByteLength, throw a RangeError exception // 7. If offset > bufferByteLength, throw a RangeError exception
if (offset->Number() > buffer_byte_length) { if (offset->Number() > buffer_byte_length) {
THROW_NEW_ERROR_RETURN_FAILURE( THROW_NEW_ERROR_RETURN_FAILURE(
isolate, isolate, NewRangeError(MessageTemplate::kInvalidOffset, offset));
NewRangeError(MessageTemplate::kInvalidDataViewOffset, offset));
} }
Handle<Object> view_byte_length; Handle<Object> view_byte_length;
......
...@@ -163,8 +163,7 @@ function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { ...@@ -163,8 +163,7 @@ function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) {
} }
newByteLength = bufferByteLength - offset; newByteLength = bufferByteLength - offset;
if (newByteLength < 0) { if (newByteLength < 0) {
throw %make_range_error(kInvalidTypedArrayAlignment, throw %make_range_error(kInvalidOffset, offset);
"byte length", "NAME", ELEMENT_SIZE);
} }
} else { } else {
newByteLength = length * ELEMENT_SIZE; newByteLength = length * ELEMENT_SIZE;
......
...@@ -508,8 +508,7 @@ class ErrorUtils : public AllStatic { ...@@ -508,8 +508,7 @@ class ErrorUtils : public AllStatic {
T(InvalidDataViewAccessorOffset, \ T(InvalidDataViewAccessorOffset, \
"Offset is outside the bounds of the DataView") \ "Offset is outside the bounds of the DataView") \
T(InvalidDataViewLength, "Invalid DataView length %") \ T(InvalidDataViewLength, "Invalid DataView length %") \
T(InvalidDataViewOffset, \ T(InvalidOffset, "Start offset % is outside the bounds of the buffer") \
"Start offset % is outside the bounds of the buffer") \
T(InvalidHint, "Invalid hint: %") \ T(InvalidHint, "Invalid hint: %") \
T(InvalidLanguageTag, "Invalid language tag: %") \ T(InvalidLanguageTag, "Invalid language tag: %") \
T(InvalidWeakMapKey, "Invalid value used as weak map key") \ T(InvalidWeakMapKey, "Invalid value used as weak map key") \
......
...@@ -389,6 +389,11 @@ test(function() { ...@@ -389,6 +389,11 @@ test(function() {
// === RangeError === // === RangeError ===
// kInvalidOffset
test(function() {
new Uint8Array(new ArrayBuffer(1),2);
}, "Start offset 2 is outside the bounds of the buffer", RangeError);
// kArrayLengthOutOfRange // kArrayLengthOutOfRange
test(function() { test(function() {
"use strict"; "use strict";
......
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