-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
test: fix test for buffer regression #649 #9924
Conversation
assert.throws(() => Buffer.allocUnsafe(len).toString('utf8')); | ||
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8')); | ||
assert.throws(() => Buffer(len).toString('utf8'), | ||
/Invalid typed array 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.
If you check the entire error message, you can add ^
and $
to the regular expression. Also, since the regex is the same for all the checks, you could move it to a variable.
d4eef26
to
e94a667
Compare
Changes addressed |
- pass a regexp to assert.throws()
e94a667
to
d6a4e3d
Compare
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8')); | ||
const lenLimitMsg = /^RangeError: Invalid typed array length$/; | ||
assert.throws(() => Buffer(len).toString('utf8'), | ||
lenLimitMsg); |
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.
All, or at least some, of these should fit on one line now.
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.
Addressed.
A noob question: is it better that I amend the commits myself during the PR so you don't have to squash them when you land them, or is it better I just leave several commits in the PR so the review process won't get hidden in GitHub?
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.
It's really a personal preference. Sometimes with large PRs it's easier for the reviewer(s) if the entire thing doesn't have to be re-reviewed after each amended change. It can be tricky though if a PR contains multiple logically divided commits and you want to amend the correct commit. For something of this size, I don't think it matters much.
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.
Got it, thanks for the explanation!
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 if it passes CI: https://ci.nodejs.org/job/node-test-pull-request/5173/
CI looks like the error message is actually platform/machine-dependent here, locally I always get Maybe just setting (And aside: |
I dug around a little bit and found out why
Here // // 1.8446744073709552E+19, that is, 0x10000000000000000 with possibly some floating number inaccuracy
double value = HeapNumber::cast(number)->value();
// on my machine the limit is 18446744073709551615, just a bit less than 18446744073709552000 = 0x10000000000000000
if (value >= 0 && value <= std::numeric_limits<size_t>::max()) {
*result = static_cast<size_t>(value); // 0
return true;
} else {
return false;
} If you add one more 0 to the number, then you will hit the |
And for that The failed call path should be The if (length > static_cast<unsigned>(Smi::kMaxValue)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewRangeError(MessageTemplate::kInvalidTypedArrayLength));
} And I believe the reason is the same as explained in typedarray.js: if (length > %_MaxSmi()) {
// Note: this is not per spec, but rather a constraint of our current
// representation (which uses smi's).
throw %make_range_error(kInvalidTypedArrayLength);
} |
So regarding to the limit of buffer length, we have two types of error:
Sometimes the huge length we set for this test is larger than the So, the results really depends on the
And I think that empty buffer returned by diff --git a/deps/v8/src/conversions-inl.h b/deps/v8/src/conversions-inl.h
index 427a67d..b78410e 100644
--- a/deps/v8/src/conversions-inl.h
+++ b/deps/v8/src/conversions-inl.h
@@ -156,6 +156,9 @@ bool TryNumberToSize(Object* number, size_t* result) {
double value = HeapNumber::cast(number)->value();
if (value >= 0 && value <= std::numeric_limits<size_t>::max()) {
*result = static_cast<size_t>(value);
+ if (value > 0 && *result == 0) {
+ return false; // cast error
+ }
return true;
} else {
return false; Noob question: should I open an issue on V8's issue tracker(and possibly subtmit a patch) or..? |
@nodejs/buffer @nodejs/v8 |
Guide on reporting / fixing V8 bugs: https://github.com/nodejs/node/blob/master/doc/guides/maintaining-V8.md#unfixed-upstream-bugs |
Verified that the bug exists in v8/node/vee-eight-lkgr and V8 master. Opened a bug https://bugs.chromium.org/p/v8/issues/detail?id=5712 and submitted a patch. |
Thanks for the in-depth explanation of what’s going on here! I think I like the third option („We can loose the test a bit and just test if the error message is one of those two.”) but maybe that’s just me wanting to go down the path of least resistance. |
Update: I looked into the C++ side of Buffer and it seems we already (kinda) do Option 1 described in my previous comments(#9924 (comment)) for MaybeLocal<Object> New(Environment* env, size_t length) {
EscapableHandleScope scope(env->isolate());
// V8 currently only allows a maximum Typed Array index of max Smi.
if (length > kMaxLength) {
return Local<Object>();
}
...
} Somehow the |
I looked around a little bit more and believe for this PR I should just go with option 3 and loose the test a bit, like what Regarding how the errors for a large length should look like, I think this deserves a separate issue. There are many tests that touch this but still don't have an accurate error message match. I'll do a write up about this there. |
Riiight… should have thought of that. We don’t use any of our own C++ code when allocating |
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.
16f7e21
to
c679654
Compare
I think the CI failed because I missed out another type of range error(as mentioned in #10152). Not sure if we are going that direction, should this PR be closed? I'll amend this PR to cover the other range error anyway. |
@joyeecheung If you manage to tweak the regexp here so that CI passes, I don’t think you need to close the PR here :) |
OK I amended the regexp here. Can you poke the CI for me again? Thanks :) |
Sure :) CI: https://ci.nodejs.org/job/node-test-commit/6484/ (edit: green!) |
pass a regexp to assert.throws() PR-URL: #9924 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
Checklist
make -j8 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
test
Description of change
pass a regexp to assert.throws()