summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/node_buffer.cc11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc
index 0f3b048272..4a0ffbbca3 100644
--- a/src/node_buffer.cc
+++ b/src/node_buffer.cc
@@ -28,6 +28,7 @@
#include "node_internals.h"
#include "env-inl.h"
+#include "simdutf.h"
#include "string_bytes.h"
#include "string_search.h"
#include "util-inl.h"
@@ -583,10 +584,20 @@ void DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
ArrayBufferViewContents<char> buffer(args[0]);
bool ignore_bom = args[1]->IsTrue();
+ bool has_fatal = args[2]->IsTrue();
const char* data = buffer.data();
size_t length = buffer.length();
+ if (has_fatal) {
+ auto result = simdutf::validate_utf8_with_errors(data, length);
+
+ if (result.error) {
+ return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
+ env->isolate(), "The encoded data was not valid for encoding utf-8");
+ }
+ }
+
if (!ignore_bom && length >= 3) {
if (memcmp(data, "\xEF\xBB\xBF", 3) == 0) {
data += 3;