Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d2b9e7c

Browse filesBrowse 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 <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 d5e1b82 commit d2b9e7c
Copy full SHA for d2b9e7c

File tree

Expand file treeCollapse file tree

2 files changed

+15
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+15
-1
lines changed
Open diff view settings
Collapse file

‎src/string_decoder.cc‎

Copy file name to clipboardExpand all lines: src/string_decoder.cc
+8-1Lines changed: 8 additions & 1 deletion
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,
Collapse file

‎test/parallel/test-string-decoder.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-string-decoder.js
+7Lines changed: 7 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.