mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
buffer: use stricter from() input validation
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: https://github.com/nodejs/node/pull/26825 Fixes: https://github.com/nodejs/node/issues/26741 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
1 parent
ef0701d31f
commit
75eaf25e78
3 files changed
+30
-59
No files matched your search
+14
-24
@@ -195,33 +195,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
|
||||
);
|
||||
}
|
||||
const valueOf = value.valueOf && value.valueOf();
|
||||
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
|
||||
return Buffer.from(valueOf, encodingOrOffset, length);
|
||||
|
||||
if (typeof value === 'number') {
|
||||
throw new ERR_INVALID_ARG_TYPE('value', 'not number', value);
|
||||
}
|
||||
const b = fromObject(value);
|
||||
if (b)
|
||||
return b;
|
||||
|
||||
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;
|
||||
|
||||
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(
|
||||
|
||||
Reference in new issue
Block a user