Skip to content

fix(buffer): Node arg-validation for alloc/allocUnsafe/concat/byteLength (#2013)#2440

Merged
proggeramlug merged 1 commit into
mainfrom
worktree-fix-2013-arg-validation
May 29, 2026
Merged

fix(buffer): Node arg-validation for alloc/allocUnsafe/concat/byteLength (#2013)#2440
proggeramlug merged 1 commit into
mainfrom
worktree-fix-2013-arg-validation

Conversation

@proggeramlug
Copy link
Copy Markdown
Contributor

What

Extends Node argument-validation parity (#2013) to the global Buffer factory methods, following the fs slice landed in #2035 / #2328. These functions previously coerced bad input silently, so assert.throws-style tests reported "Missing expected exception".

API Bad input Now throws
Buffer.alloc / allocUnsafe / allocUnsafeSlow non-number size TypeError [ERR_INVALID_ARG_TYPE]
NaN / Infinity / negative / > kMaxLength RangeError [ERR_OUT_OF_RANGE]
Buffer.concat non-Array list TypeError [ERR_INVALID_ARG_TYPE]
non-Buffer/Uint8Array element TypeError [ERR_INVALID_ARG_TYPE] (list[i])
non-integer / negative totalLength ERR_INVALID_ARG_TYPE / ERR_OUT_OF_RANGE
Buffer.byteLength non string/Buffer/ArrayBuffer/TypedArray TypeError [ERR_INVALID_ARG_TYPE]

Non-integer sizes still truncate toward zero (Buffer.alloc(2.5).length === 2), matching Node.

How

  • Reuses the fs::validate primitives (describe_received, throw_type_error_with_code, throw_range_error_with_code, validate_int32) — the issue explicitly names this the shared validation home. describe_received gains string/bigint rendering (Node's determineSpecificType shape) — purely additive; fs never reaches those branches.
  • New buffer/validate.rs with js_buffer_validate_size, js_buffer_validate_concat_list, validate_concat_length, validate_byte_length_arg.
  • The size/list values reach validation as raw NaN-boxed values: the Buffer factory codegen (env_clones.rs, array_methods.rs) now calls the validators before coercing to i32 / treating as an ArrayHeader (the latter also closes a latent UB where Buffer.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 to undefined so they surface ERR_INVALID_ARG_TYPE like Node, instead of silently allocating empty.

Testing

Verified byte-identical to Node v25 across the full buffer node-suite (120 cases, including 3 new arg-validation tests under allocation/, concat/, byte-length/) — 0 diffs, 0 regressions. cargo fmt --all --check clean.

Scope

#2013 is a cross-cutting umbrella (~85 tests across fs/buffer/net/http/crypto/zlib/process/url). fs landed earlier; this is the buffer slice. Buffer.compare is deferred (it lowers to an instance a.compare(b) dispatch — a separate path); remaining APIs remain follow-ups.

…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.
@proggeramlug proggeramlug merged commit 5d534a1 into main May 29, 2026
11 checks passed
@proggeramlug proggeramlug deleted the worktree-fix-2013-arg-validation branch May 29, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant