-
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
Buffer.alloc(): A TypeError will be thrown if size is not a number.
#26151
Comments
The reason the ThirdPartyModule = { HEADERSIZE: 64 };
let size = 1000; // Pretend the user has validated this already.
Buffer.alloc(ThirdPartyModule.HEADER_SIZE + size); And then pass this buffer to a native addon, assuming Since the user miss-typed If the native addon then fails to check the Granted, this is alot of "ifs", but |
As an aside, and just to highlight this again, the range check in size < 0 || size > kMaxLength There are too many possible values of This actually checks the range: !(size >= 0 && size <= kMaxLength) |
I think this is a bug. |
@Fishrock123 Does that mean you think #26162 shouldn’t be semver-major? I’d still say that it is… |
Buffer.alloc(size)
and friends (allocUnsafe()
,allocUnsafeSlow()
) promise that:However, this contract is broken when
size
isNaN
:The reason is that
assertSize()
, which validates thesize
argument, does the check usingtypeof size !== 'number'
, however:Number.isInteger()
would be better:Alternatively, a faster test for
NaN
would be swapping the range check around to throw for anything not in range (as opposed to throw for anything before 0 or afterkMaxLength
which is what it currently does):The above snippet catches
NaN
.The text was updated successfully, but these errors were encountered: