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;
}

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 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);
Loading