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

string_decoder: throw an error when writing a too long buffer #52215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/string_decoder.cc
Expand Up @@ -31,11 +31,11 @@ MaybeLocal<String> MakeString(Isolate* isolate,
Local<Value> error;
MaybeLocal<Value> ret;
if (encoding == UTF8) {
MaybeLocal<String> utf8_string = String::NewFromUtf8(
isolate,
data,
v8::NewStringType::kNormal,
length);
MaybeLocal<String> utf8_string;
if (length <= static_cast<size_t>(v8::String::kMaxLength)) {
utf8_string = String::NewFromUtf8(
isolate, data, v8::NewStringType::kNormal, length);
}
if (utf8_string.IsEmpty()) {
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
return MaybeLocal<String>();
Expand Down
29 changes: 29 additions & 0 deletions test/pummel/test-string-decoder-large-buffer.js
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common');

// Buffer with size > INT32_MAX
common.skipIf32Bits();

const assert = require('assert');
const { StringDecoder } = require('node:string_decoder');
const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;

const size = 2 ** 31;
theanarkh marked this conversation as resolved.
Show resolved Hide resolved

const stringTooLongError = {
message: `Cannot create a string longer than 0x${kStringMaxLength.toString(16)}` +
' characters',
code: 'ERR_STRING_TOO_LONG',
name: 'Error',
};

try {
const buf = Buffer.allocUnsafe(size);
const decoder = new StringDecoder('utf8');
assert.throws(() => decoder.write(buf), stringTooLongError);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
}
common.skip('insufficient space for Buffer.alloc');
}