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

fix: RangeError [ERR_BUFFER_OUT_OF_BOUNDS] "length" is outside of buffer bounds #2026

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node_version: ["12", "14", "16", "18"]
node_version: ["12", "14", "16", "18", "20", "22"]
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
Expand Down
18 changes: 16 additions & 2 deletions src/writer_buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ BufferWriter.prototype.bytes = function write_bytes_buffer(value) {
return this;
};

const isOlderVer = compareVersions(process.version.slice(1), targetVersion) < 0 ? true : false;

function writeStringBuffer(val, buf, pos) {
const targetVersion = '22.7.0';
if (val.length < 40) // plain js is faster for short strings (probably due to redundant assertions)
util.utf8.write(val, buf, pos);
else if (buf.utf8Write)
buf.utf8Write(val, pos);
else if (isOlderVer && buf.utf8Write)
buf.utf8Write(val, pos); // node less than 22.7.0
else
buf.write(val, pos);
}
Expand All @@ -83,3 +86,14 @@ BufferWriter.prototype.string = function write_string_buffer(value) {
*/

BufferWriter._configure();

function compareVersions(version1, version2) {
const v1Parts = version1.split('.').map(Number);
const v2Parts = version2.split('.').map(Number);

for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i+=1) {
if ((v1Parts[i] || 0) < (v2Parts[i] || 0)) { return -1 }
if ((v1Parts[i] || 0) > (v2Parts[i] || 0)) { return 1 }
}
return 0;
}