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 ERR_STRING_TOO_LONG for UTF-8 #36661

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
9 changes: 8 additions & 1 deletion src/string_decoder.cc
Expand Up @@ -3,6 +3,7 @@

#include "env-inl.h"
#include "node_buffer.h"
#include "node_errors.h"
#include "node_external_reference.h"
#include "string_bytes.h"
#include "util.h"
Expand Down Expand Up @@ -30,11 +31,17 @@ MaybeLocal<String> MakeString(Isolate* isolate,
Local<Value> error;
MaybeLocal<Value> ret;
if (encoding == UTF8) {
return String::NewFromUtf8(
MaybeLocal<String> 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>();
} else {
return utf8_string;
}
} else {
ret = StringBytes::Encode(
isolate,
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-string-decoder.js
Expand Up @@ -201,6 +201,13 @@ assert.throws(
}
);

assert.throws(
() => new StringDecoder().write(Buffer.alloc(0x1fffffe8 + 1).fill('a')),
{
code: 'ERR_STRING_TOO_LONG',
}
);

// Test verifies that StringDecoder will correctly decode the given input
// buffer with the given encoding to the expected output. It will attempt all
// possible ways to write() the input buffer, see writeSequences(). The
Expand Down