denoland/deno · error · NodeRangeError
ERR_BUFFER_OUT_OF_BOUNDS
ERR_BUFFER_OUT_OF_BOUNDS
Error message
"offset" is outside of buffer bounds
What it means
In sliceBuffer(), after offset is coerced with >>> 0, an offset greater than buffer.byteLength throws ERR_BUFFER_OUT_OF_BOUNDS('offset'). The datagram slice must start inside the buffer, so the requested window has to begin at or before the last byte.
Solutions
- Validate offset <= buf.byteLength before send (use 0 for an empty payload)
- Clamp: offset = Math.min(Math.max(offset, 0), buf.byteLength)
- Compute length as the remaining bytes: length = buf.byteLength - offset
Example fix
// before socket.send(buf, HEADER_SIZE, buf.byteLength, PORT, HOST); // throws when buf is only a header // after const offset = Math.min(HEADER_SIZE, buf.byteLength); socket.send(buf, offset, buf.byteLength - offset, PORT, HOST);
Defensive patterns
Strategy: validation
Validate before calling
function sendSlice(socket, buf, offset, length, port, address) {
offset = Math.max(0, Math.min(offset >>> 0, buf.byteLength));
length = Math.min(length >>> 0, buf.byteLength - offset);
socket.send(buf, offset, length, port, address);
} Prevention
- Clamp offset to [0, buf.byteLength] before send
- Remember >>> 0 turns negative offsets into huge unsigned values
- Derive offsets from the actual buffer being sent, never from a parallel variable
When it happens
Trigger: socket.send(buf, 100, 10, port, addr) when buf.byteLength is 50; a negative offset (-1) that wraps to 4294967295 via >>> 0; framing code subtracting a header size larger than short packets.
Common situations: Protocol parsers assuming a fixed minimum packet size; offset computed from a different buffer than the one sent; negative offsets from arithmetic bugs.
Related errors
- ERR_INVALID_ARG_TYPE
- ERR_INVALID_FD_TYPE
- ERR_MISSING_ARGS
- ERR_SOCKET_ALREADY_BOUND
- ERR_SOCKET_BAD_BUFFER_SIZE
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/2a47a549773d38f3.
Report an issue: GitHub.
Appendix: source
Thrown at ext/node/polyfills/dgram.ts:1473
}
function sliceBuffer(buffer: MessageType, offset: number, length: number) {
if (typeof buffer === "string") {
buffer = Buffer.from(buffer);
} else if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE(
"buffer",
["Buffer", "TypedArray", "DataView", "string"],
buffer,
);
}
offset = offset >>> 0;
length = length >>> 0;
// deno-lint-ignore deno-internal/prefer-primordials -- buffer may be a Buffer or DataView, not a plain TypedArray
if (offset > buffer.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS("offset");
}
// deno-lint-ignore deno-internal/prefer-primordials -- buffer may be a Buffer or DataView, not a plain TypedArray
if (offset + length > buffer.byteLength) {
throw new ERR_BUFFER_OUT_OF_BOUNDS("length");
}
// deno-lint-ignore deno-internal/prefer-primordials -- Buffer is the Node Buffer class; .buffer/.byteOffset on a Buffer or DataView
return Buffer.from(buffer.buffer, buffer.byteOffset + offset, length);
}
function fixBufferList(
list: ReadonlyArray<MessageType>,
): Array<MessageType> | null {
const newList = new Array(list.length);
for (let i = 0, l = list.length; i < l; i++) {
const buf = list[i];View on GitHub (pinned to 9ad36f7a2c)