Skip to content
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
4 changes: 3 additions & 1 deletion src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "string_bytes.h"
#include "v8.h"

#include <algorithm>
#include <climits>
#include <cstdint>

namespace node {
Expand Down Expand Up @@ -102,7 +104,7 @@ void BindingData::EncodeInto(const FunctionCallbackInfo<Value>& args) {
int written = source->WriteUtf8(
isolate,
write_result,
dest_length,
std::min(dest_length, static_cast<size_t>(INT_MAX)),
&nchars,
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8);

Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-whatwg-encoding-encodeinto-large.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const common = require('../common');

common.skipIf32Bits();

const assert = require('assert');

const encoder = new TextEncoder();
const source = 'a\xFF\u6211\u{1D452}';
const expected = encoder.encode(source);

const size = 2 ** 31 + expected.length;
const offset = expected.length - 1;
let dest;

try {
dest = new Uint8Array(size);
} catch (e) {
if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)) {
common.skip('insufficient space for Uint8Array allocation');
}
throw e;
Comment on lines +20 to +24
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for a specific error condition is unnecessary here – there is no other error that can occur when calling new TypedArray(n) with a safe integer.

}

const large = encoder.encodeInto(source, dest.subarray(offset));
assert.deepStrictEqual(large, {
read: source.length,
written: expected.length,
});
assert.deepStrictEqual(dest.slice(offset, offset + expected.length), expected);
Comment on lines +27 to +32
Copy link
Copy Markdown
Member

@Renegade334 Renegade334 Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's really no need to mess around with creating a typed array and then deriving a slightly shorter array that's still larger than INT_MAX. Just make the typed array with size 2 ** 31 at the start, and use it directly.

Suggested change
const large = encoder.encodeInto(source, dest.subarray(offset));
assert.deepStrictEqual(large, {
read: source.length,
written: expected.length,
});
assert.deepStrictEqual(dest.slice(offset, offset + expected.length), expected);
const result = encoder.encodeInto(source, dest);
assert.deepStrictEqual(result, {
read: source.length,
written: expected.length,
});
assert.deepStrictEqual(dest.subarray(0, expected.length), expected);


const bounded = encoder.encodeInto(source,
dest.subarray(offset,
offset + expected.length));
assert.deepStrictEqual(bounded, {
read: source.length,
written: expected.length,
});
assert.deepStrictEqual(dest.slice(offset, offset + expected.length), expected);
Comment on lines +34 to +41
Copy link
Copy Markdown
Member

@Renegade334 Renegade334 Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is unnecessary – it isn't testing passing a large typed array to encodeInto, and succeeds both before and after the change.

Loading