Skip to content

Buffer.prototype.asciiSlice, etc use offset, length instead of start, end #2538

@mhart

Description

@mhart

The following is fine in Node.js:

> Buffer.from('abcd').utf8Slice(2, 3).toString()
'c'

But in workerd it throws:

The value of "length" is out of range. It must be >= 0 && <= 2. Received 3

Relevant source for us:

Buffer.prototype.asciiSlice = function asciiSlice(offset: number, length: number) {
validateOffset(offset, "offset", 0, this.length);
validateOffset(length, "length", 0, this.length - offset);
return bufferUtil.toString(this, offset, offset + length, ASCII);
};
Buffer.prototype.base64Slice = function base64Slice(offset: number, length: number) {
validateOffset(offset, "offset", 0, this.length);
validateOffset(length, "length", 0, this.length - offset);
return bufferUtil.toString(this, offset, offset + length, BASE64);
};
Buffer.prototype.base64urlSlice = function base64urlSlice(offset: number, length: number) {
validateOffset(offset, "offset", 0, this.length);
validateOffset(length, "length", 0, this.length - offset);
return bufferUtil.toString(this, offset, offset + length, BASE64URL);
};
Buffer.prototype.hexSlice = function hexSlice(offset: number, length: number) {
validateOffset(offset, "offset", 0, this.length);
validateOffset(length, "length", 0, this.length - offset);
return bufferUtil.toString(this, offset, offset + length, HEX);
};
Buffer.prototype.latin1Slice = function latin1Slice(offset: number,
length: number) {
validateOffset(offset, "offset", 0, this.length);
validateOffset(length, "length", 0, this.length - offset);
return bufferUtil.toString(this, offset, offset + length, LATIN1);
};
Buffer.prototype.ucs2Slice = function ucs2Slice(offset: number, length: number) {
validateOffset(offset, "offset", 0, this.length);
validateOffset(length, "length", 0, this.length - offset);
return bufferUtil.toString(this, offset, offset + length, UTF16LE);
};
Buffer.prototype.utf8Slice = function utf8Slice(offset: number, length: number) {
validateOffset(offset, "offset", 0, this.length);
validateOffset(length, "length", 0, this.length - offset);
return bufferUtil.toString(this, offset, offset + length, UTF8);
};

Relevant source for Node.js:

https://github.com/nodejs/node/blob/8d3758487498e3c1edfd0f6f4d36d0fb3a3fab4e/src/node_buffer.cc#L545-L551

(Note also that Node.js allows end < start so long as both are not out of bounds and sets:if (end < start) end = start)

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions