diff --git a/src/encoding_binding.cc b/src/encoding_binding.cc index cddc88f1e03090..5e0606352d3587 100644 --- a/src/encoding_binding.cc +++ b/src/encoding_binding.cc @@ -8,6 +8,8 @@ #include "string_bytes.h" #include "v8.h" +#include +#include #include namespace node { @@ -102,7 +104,7 @@ void BindingData::EncodeInto(const FunctionCallbackInfo& args) { int written = source->WriteUtf8( isolate, write_result, - dest_length, + std::min(dest_length, static_cast(INT_MAX)), &nchars, String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8); diff --git a/test/parallel/test-whatwg-encoding-encodeinto-large.js b/test/parallel/test-whatwg-encoding-encodeinto-large.js new file mode 100644 index 00000000000000..09d9caa411e18a --- /dev/null +++ b/test/parallel/test-whatwg-encoding-encodeinto-large.js @@ -0,0 +1,26 @@ +'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); + +let dest; + +try { + dest = new Uint8Array(2 ** 31); +} catch { + common.skip('insufficient space for Uint8Array allocation'); +} + +const result = encoder.encodeInto(source, dest); +assert.deepStrictEqual(result, { + read: source.length, + written: expected.length, +}); +assert.deepStrictEqual(dest.subarray(0, expected.length), expected);