diff --git a/src/string_decoder.cc b/src/string_decoder.cc index 6ec84e0e11ed31..779d60356d609d 100644 --- a/src/string_decoder.cc +++ b/src/string_decoder.cc @@ -3,6 +3,7 @@ #include "env-inl.h" #include "node_buffer.h" +#include "node_errors.h" #include "string_bytes.h" #include "util.h" @@ -29,11 +30,17 @@ MaybeLocal MakeString(Isolate* isolate, Local error; MaybeLocal ret; if (encoding == UTF8) { - return String::NewFromUtf8( + MaybeLocal utf8_string = String::NewFromUtf8( isolate, data, v8::NewStringType::kNormal, length); + if (utf8_string.IsEmpty()) { + isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate)); + return MaybeLocal(); + } else { + return utf8_string; + } } else { ret = StringBytes::Encode( isolate, diff --git a/test/parallel/test-string-decoder.js b/test/parallel/test-string-decoder.js index 1fbf9b572cc2f8..043816bdbb1568 100644 --- a/test/parallel/test-string-decoder.js +++ b/test/parallel/test-string-decoder.js @@ -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