Skip to content

Commit 1dc4dea

Browse files
committed
string_decoder: throw ERR_STRING_TOO_LONG for UTF-8
String::NewFromUtf8 doesn't generate an exception in V8 when the string is too long but is guaranteed to return an empty MaybeLocal only in that case. Generate a Node.js exception when it happens. Fixes: #35676 PR-URL: #36661 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3215a58 commit 1dc4dea

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/string_decoder.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "env-inl.h"
55
#include "node_buffer.h"
6+
#include "node_errors.h"
67
#include "string_bytes.h"
78
#include "util.h"
89

@@ -29,11 +30,17 @@ MaybeLocal<String> MakeString(Isolate* isolate,
2930
Local<Value> error;
3031
MaybeLocal<Value> ret;
3132
if (encoding == UTF8) {
32-
return String::NewFromUtf8(
33+
MaybeLocal<String> utf8_string = String::NewFromUtf8(
3334
isolate,
3435
data,
3536
v8::NewStringType::kNormal,
3637
length);
38+
if (utf8_string.IsEmpty()) {
39+
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
40+
return MaybeLocal<String>();
41+
} else {
42+
return utf8_string;
43+
}
3744
} else {
3845
ret = StringBytes::Encode(
3946
isolate,

test/parallel/test-string-decoder.js

+7
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ assert.throws(
201201
}
202202
);
203203

204+
assert.throws(
205+
() => new StringDecoder().write(Buffer.alloc(0x1fffffe8 + 1).fill('a')),
206+
{
207+
code: 'ERR_STRING_TOO_LONG',
208+
}
209+
);
210+
204211
// Test verifies that StringDecoder will correctly decode the given input
205212
// buffer with the given encoding to the expected output. It will attempt all
206213
// possible ways to write() the input buffer, see writeSequences(). The

0 commit comments

Comments
 (0)