Skip to content

Commit d2b9e7c

Browse files
targosdanielleadams
authored andcommitted
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 <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d5e1b82 commit d2b9e7c

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 "node_external_reference.h"
78
#include "string_bytes.h"
89
#include "util.h"
@@ -30,11 +31,17 @@ MaybeLocal<String> MakeString(Isolate* isolate,
3031
Local<Value> error;
3132
MaybeLocal<Value> ret;
3233
if (encoding == UTF8) {
33-
return String::NewFromUtf8(
34+
MaybeLocal<String> utf8_string = String::NewFromUtf8(
3435
isolate,
3536
data,
3637
v8::NewStringType::kNormal,
3738
length);
39+
if (utf8_string.IsEmpty()) {
40+
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
41+
return MaybeLocal<String>();
42+
} else {
43+
return utf8_string;
44+
}
3845
} else {
3946
ret = StringBytes::Encode(
4047
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)