Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mhart opened this issue Aug 16, 2024 · 0 comments · Fixed by #2541
Closed

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

mhart opened this issue Aug 16, 2024 · 0 comments · Fixed by #2541
Assignees
Labels
bug Something isn't working nodejs compat

Comments

@mhart
Copy link
Collaborator

mhart commented Aug 16, 2024

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)

@anonrig anonrig self-assigned this Aug 16, 2024
@jasnell jasnell added bug Something isn't working nodejs compat labels Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working nodejs compat
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants