-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
n-api: throw RangeError in napi_create_typedarray() with invalid args #18037
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mhdawson (or others) PTAL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a few nits.
doc/api/errors.md
Outdated
<a id="ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT"></a> | ||
### ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT | ||
|
||
While calling `napi_create_typedarray()`, a given `offset` was not a multiple of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change "a given" to "the provided"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
doc/api/errors.md
Outdated
### ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT | ||
|
||
While calling `napi_create_typedarray()`, a given `offset` was not a multiple of | ||
size of element of given type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change "size of element of given type" to "the element size"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
doc/api/errors.md
Outdated
### ERR_NAPI_INVALID_TYPEDARRAY_LENGTH | ||
|
||
While calling `napi_create_typedarray()`, `(length * size_of_element) + | ||
byte_offset` was larger than a length of given `buffer`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"a length of given" -> "the length of"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
const template = Reflect.construct(currentType, buffer); | ||
assert.throws(() => { | ||
test_typedarray.CreateTypedArray(template, buffer, 0, 129); | ||
}, RangeError); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of just checking for RangeError
, can you also validate the error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
3677e16
to
48503c6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for review.
I addressed all your comments.
Another look?
doc/api/errors.md
Outdated
<a id="ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT"></a> | ||
### ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT | ||
|
||
While calling `napi_create_typedarray()`, a given `offset` was not a multiple of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
doc/api/errors.md
Outdated
### ERR_NAPI_INVALID_TYPEDARRAY_ALIGNMENT | ||
|
||
While calling `napi_create_typedarray()`, a given `offset` was not a multiple of | ||
size of element of given type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
doc/api/errors.md
Outdated
### ERR_NAPI_INVALID_TYPEDARRAY_LENGTH | ||
|
||
While calling `napi_create_typedarray()`, `(length * size_of_element) + | ||
byte_offset` was larger than a length of given `buffer`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
const template = Reflect.construct(currentType, buffer); | ||
assert.throws(() => { | ||
test_typedarray.CreateTypedArray(template, buffer, 0, 129); | ||
}, RangeError); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Will leave a bit more time for comments and then kick of CI run tomorrow. |
@romandev either the test does not handle big endian or we have a bug as one of the tests failed on all of the Big Endian platforms: https://ci.nodejs.org/job/node-test-commit-plinux/14443/nodes=ppcbe-ubuntu1404/console not ok 1876 addons-napi/test_typedarray/testduration_ms: 0.173
... |
Should I use virtual machine to test on the platforms? or is there a better way? |
NAPI_ASSERT(env, valuetype2 == napi_number, | ||
"Wrong type of arguments. Expects a number as third argument."); | ||
|
||
NAPI_CALL(env, napi_get_value_uint32(env, args[2], (uint32_t*)(&length))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cast won't work on big-endian. So something like this maybe?
uint32_t locallength = length;
NAPI_CALL(..., &locallength);
length = locallength;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for input. Done.
"Wrong type of arguments. Expects a number as third argument."); | ||
|
||
NAPI_CALL(env, napi_get_value_uint32( | ||
env, args[3], (uint32_t*)(&byte_offset))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made some comments about some explicit pointer casts that might be causing the big endian issues.
According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length
@mhdawson Thank you for triggering CI.
|
CI failure is known issue -> #18160 and not related. CI looks clean. |
According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length PR-URL: #18037 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
Landed as 316b5ef |
According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length PR-URL: #18037 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
Should this be backported to |
According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length PR-URL: nodejs#18037 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length PR-URL: nodejs#18037 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length Backport-PR-URL: #19447 PR-URL: #18037 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
According to the ECMA spec, we should throw a RangeError in the following cases: - `(length * elementSize) + offset` > the size of the array passed in - `offset % elementSize` != `0` In the current implementation, this check was omitted. So, the following code will cause a crash. ``` napi_create_typedarray(env, napi_uint16_array, 2 /* length */, buffer, 1 /* byte_offset */, &output_array); ``` This change fixes the problem and write some related tests. Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length Backport-PR-URL: #19265 PR-URL: #18037 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
According to the ECMA spec, we should throw a RangeError in the
following cases:
(length * elementSize) + offset > the size of the array passed in
offset % elementSize != 0
In the current implementation, this check was omitted. So, the following
code will cause a crash.
This change fixes the problem and write some related tests.
Refs: https://tc39.github.io/ecma262/#sec-typedarray-buffer-byteoffset-length
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
n-api