-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
Buffer.from(function) should probably fail #26741
Comments
Sorry @ChALkeR. I didn't read the entire issue after I saw how to reproduce it. I'll close my PR. |
@ChALkeR to me it seems like we should also throw on all primitives besides strings. That would also simplify the logic: diff --git a/lib/buffer.js b/lib/buffer.js
index c5497e18aa..4c00652b50 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -192,33 +192,23 @@ Buffer.from = function from(value, encodingOrOffset, length) {
if (typeof value === 'string')
return fromString(value, encodingOrOffset);
- if (isAnyArrayBuffer(value))
- return fromArrayBuffer(value, encodingOrOffset, length);
+ if (typeof value === 'object' && value !== null) {
+ if (isAnyArrayBuffer(value))
+ return fromArrayBuffer(value, encodingOrOffset, length);
- if (value === null || value === undefined) {
- throw new ERR_INVALID_ARG_TYPE(
- 'first argument',
- ['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
- value
- );
- }
-
- if (typeof value === 'number') {
- throw new ERR_INVALID_ARG_TYPE('value', 'not number', value);
- }
+ const valueOf = value.valueOf && value.valueOf();
+ if (valueOf !== null && valueOf !== undefined && valueOf !== value)
+ return Buffer.from(valueOf, encodingOrOffset, length);
- const valueOf = value.valueOf && value.valueOf();
- if (valueOf !== null && valueOf !== undefined && valueOf !== value)
- return Buffer.from(valueOf, encodingOrOffset, length);
-
- const b = fromObject(value);
- if (b)
- return b;
+ const b = fromObject(value);
+ if (b)
+ return b;
- if (typeof value[Symbol.toPrimitive] === 'function') {
- return Buffer.from(value[Symbol.toPrimitive]('string'),
- encodingOrOffset,
- length);
+ if (typeof value[Symbol.toPrimitive] === 'function') {
+ return Buffer.from(value[Symbol.toPrimitive]('string'),
+ encodingOrOffset,
+ length);
+ }
}
throw new ERR_INVALID_ARG_TYPE( |
@cjihrig Um, there was no reason to close your PR if you already drafted that! Less work for me =). I have not yet started doing anything, just wanted to collect initial feedback, so if you already have a PR, it would make sense reopening it, I think? The PR looks good at the first glance, I will review it later if you reopen it. 😉 |
@BridgeAR could you give a list of examples which behavior get changed by that? |
@ChALkeR currently symbols cause an maximum call stack size error and the error message for numbers is different than from the other primitives (and less helpful). For bigint and booleans it's only about executing less code. Currently we try to create something useful from those types and if that fails, we'll throw the error. |
|
So far we did not throw an error for all types of invalid input. Functions do not return a buffer anymore and `number` and `symbol` validation is also improved. PR-URL: #26825 Fixes: #26741 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
So far we did not throw an error for all types of invalid input. Functions do not return a buffer anymore and `number` and `symbol` validation is also improved. PR-URL: #26825 Fixes: #26741 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Signed-off-by: Beth Griggs <[email protected]>
Current behavior of
Buffer.from
when a function is passed as the first argument:That doesn't seem useful, so passing a function in is most likely a programmers error, so we could throw errors on that to indicate those early. Thoughts?
If there are no immediate objections, I'll make a draft PR.
The text was updated successfully, but these errors were encountered: