🐛 Bug Report Runtime APIs node:buffer: Buffer.alloc and Buffer.allocUnsafe throw ERR_OUT_OF_RANGE for size === kMaxLength (off-by-one)
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 88/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- node.js, typescript
- Domain
- api
Research direction
Start in src/node/internal/internal_buffer.ts and compare the alloc() and createBuffer() boundary checks described in the report. Verify that the maximum valid size is accepted and one value above it still raises ERR_OUT_OF_RANGE, with memory constraints considered when running the reproduction.
Written by the indexing model from the issue text.
Description
Description
Buffer.alloc(size) and Buffer.allocUnsafe(size) incorrectly throw ERR_OUT_OF_RANGE when size === buffer.kMaxLength (2147483647), because the check uses >= instead of >. The internal createBuffer function correctly uses >, so there is an inconsistency within the same file.
Reproduction
js
import { kMaxLength } from 'node:buffer';
console.log(kMaxLength); // 2147483647
// These should succeed (kMaxLength is the maximum valid size):
Buffer.alloc(kMaxLength); // throws ERR_OUT_OF_RANGE in workerd
Buffer.allocUnsafe(kMaxLength); // throws ERR_OUT_OF_RANGE in workerd
// This correctly throws (size exceeds maximum):
Buffer.alloc(kMaxLength + 1); // throws — correct
Expected behavior (Node.js v22)
Buffer.alloc(kMaxLength) and Buffer.allocUnsafe(kMaxLength) succeed (memory permitting). Only size > kMaxLength should throw ERR_OUT_OF_RANGE.
Actual behavior (workerd)
Both throw:
RangeError [ERR_OUT_OF_RANGE]: The value of "size" is out of range.
It must be >= 0 and <= 2147483647. Received 2147483647
Note that the error message itself says "0 to 2147483647" is valid, confirming that 2147483647 should not throw.
Root cause
In src/node/internal/internal_buffer.ts, the alloc() function uses >=:
ts
// BUG — uses >= so kMaxLength throws
if (size >= kMaxLength) {
throw new ERR_OUT_OF_RANGE('size', 0 to ${kMaxLength}, size);
}
But createBuffer() in the same file correctly uses >:
ts
// CORRECT
if (length > kMaxLength) {
throw new ERR_OUT_OF_RANGE(...);
}
Fix: Change size >= kMaxLength to size > kMaxLength in alloc().
Impact
Any code calling Buffer.alloc(buffer.kMaxLength) incorrectly receives ERR_OUT_OF_RANGE when the size is valid.
- Dominant language
- C++
- Stars
- 8.8k
- Forks
- 744
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 205
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudflare/workerd
-
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
cloudflare/workerd#7282 ·
-
[Miniflare] Cross-worker Durable Object proxy drops ctx.id.name for IDs created with idFromName() Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
cloudflare/workerd#7286 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
cloudflare/workerd#7026 · 2 comments · 1 reaction ·
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
cloudflare/workerd#6921 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
cloudflare/workerd#6920 ·
All issues in cloudflare/workerd
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
NVIDIA/cuda-samples#453 ·