From 1dc4dea215c2df88b5d4db8b0e7a38dd912197e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Mon, 28 Dec 2020 14:18:48 +0100 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/35676 PR-URL: https://github.com/nodejs/node/pull/36661 Reviewed-By: Anna Henningsen Reviewed-By: Gireesh Punathil Reviewed-By: Rich Trott Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/string_decoder.cc | 9 ++++++++- test/parallel/test-string-decoder.js | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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