fix(buffer): Node arg-validation for alloc/allocUnsafe/concat/byteLength (#2013)#2440
Merged
Merged
Conversation
…gth (#2013) Buffer.alloc/allocUnsafe/allocUnsafeSlow now throw ERR_INVALID_ARG_TYPE on a non-number size and ERR_OUT_OF_RANGE on NaN/Infinity/negatives; Buffer.concat throws ERR_INVALID_ARG_TYPE for a non-Array list / non-Buffer element and validates totalLength; Buffer.byteLength rejects a non string/Buffer/ ArrayBuffer/TypedArray first argument. Previously these coerced silently, so assert.throws-style tests saw 'Missing expected exception'. Reuses the fs::validate primitives (#2035) — the shared validation home the issue calls out — extending describe_received to render strings/bigints. New runtime validators are reached from the Buffer factory codegen (env_clones.rs / array_methods.rs) and the byteLength/concat runtime paths. Verified byte-identical to Node v25 across 120 buffer node-suite cases (incl. 3 new arg-validation tests); no regressions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Extends Node argument-validation parity (#2013) to the global
Bufferfactory methods, following thefsslice landed in #2035 / #2328. These functions previously coerced bad input silently, soassert.throws-style tests reported "Missing expected exception".Buffer.alloc/allocUnsafe/allocUnsafeSlowsizeTypeError [ERR_INVALID_ARG_TYPE]NaN/Infinity/ negative /> kMaxLengthRangeError [ERR_OUT_OF_RANGE]Buffer.concatlistTypeError [ERR_INVALID_ARG_TYPE]TypeError [ERR_INVALID_ARG_TYPE](list[i])totalLengthERR_INVALID_ARG_TYPE/ERR_OUT_OF_RANGEBuffer.byteLengthTypeError [ERR_INVALID_ARG_TYPE]Non-integer sizes still truncate toward zero (
Buffer.alloc(2.5).length === 2), matching Node.How
fs::validateprimitives (describe_received,throw_type_error_with_code,throw_range_error_with_code,validate_int32) — the issue explicitly names this the shared validation home.describe_receivedgains string/bigint rendering (Node'sdetermineSpecificTypeshape) — purely additive;fsnever reaches those branches.buffer/validate.rswithjs_buffer_validate_size,js_buffer_validate_concat_list,validate_concat_length,validate_byte_length_arg.size/listvalues reach validation as raw NaN-boxed values: the Buffer factory codegen (env_clones.rs,array_methods.rs) now calls the validators before coercing toi32/ treating as anArrayHeader(the latter also closes a latent UB whereBuffer.concat('x')cast a string pointer to an array header).byteLength/concat(totalLength)validate inside the runtime entry points.Buffer.alloc()/allocUnsafe()with no argument now default toundefinedso they surfaceERR_INVALID_ARG_TYPElike Node, instead of silently allocating empty.Testing
Verified byte-identical to Node v25 across the full
buffernode-suite (120 cases, including 3 new arg-validation tests underallocation/,concat/,byte-length/) — 0 diffs, 0 regressions.cargo fmt --all --checkclean.Scope
#2013 is a cross-cutting umbrella (~85 tests across fs/buffer/net/http/crypto/zlib/process/url).
fslanded earlier; this is the buffer slice.Buffer.compareis deferred (it lowers to an instancea.compare(b)dispatch — a separate path); remaining APIs remain follow-ups.