2323
2424#include " base64.h"
2525#include " node_internals.h"
26+ #include " node_errors.h"
2627#include " node_buffer.h"
2728
2829#include < limits.h>
3637// use external string resources.
3738#define EXTERN_APEX 0xFBEE9
3839
39- // TODO(addaleax): These should all have better error messages. In particular,
40- // they should mention what the actual limits are.
41- #define SB_MALLOC_FAILED_ERROR \
42- v8::Exception::Error (OneByteString(isolate, " \" toString()\" failed" ))
43-
44- #define SB_STRING_TOO_LONG_ERROR \
45- v8::Exception::Error (OneByteString(isolate, " \" toString()\" failed" ))
46-
47- #define SB_BUFFER_CREATION_ERROR \
48- v8::Exception::Error (OneByteString(isolate, " \" toString()\" failed" ))
49-
50- #define SB_BUFFER_SIZE_EXCEEDED_ERROR \
51- v8::Exception::Error (OneByteString(isolate, " \" toString()\" failed" ))
52-
5340namespace node {
5441
5542using v8::HandleScope;
@@ -93,7 +80,7 @@ class ExternString: public ResourceType {
9380
9481 TypeName* new_data = node::UncheckedMalloc<TypeName>(length);
9582 if (new_data == nullptr ) {
96- *error = SB_MALLOC_FAILED_ERROR ;
83+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
9784 return MaybeLocal<Value>();
9885 }
9986 memcpy (new_data, data, length * sizeof (*new_data));
@@ -126,7 +113,7 @@ class ExternString: public ResourceType {
126113
127114 if (str.IsEmpty ()) {
128115 delete h_str;
129- *error = SB_STRING_TOO_LONG_ERROR ;
116+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
130117 return MaybeLocal<Value>();
131118 }
132119
@@ -183,7 +170,7 @@ MaybeLocal<Value> ExternOneByteString::NewSimpleFromCopy(Isolate* isolate,
183170 v8::NewStringType::kNormal ,
184171 length);
185172 if (str.IsEmpty ()) {
186- *error = SB_STRING_TOO_LONG_ERROR ;
173+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
187174 return MaybeLocal<Value>();
188175 }
189176 return str.ToLocalChecked ();
@@ -201,7 +188,7 @@ MaybeLocal<Value> ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate,
201188 v8::NewStringType::kNormal ,
202189 length);
203190 if (str.IsEmpty ()) {
204- *error = SB_STRING_TOO_LONG_ERROR ;
191+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
205192 return MaybeLocal<Value>();
206193 }
207194 return str.ToLocalChecked ();
@@ -616,7 +603,7 @@ static size_t hex_encode(const char* src, size_t slen, char* dst, size_t dlen) {
616603#define CHECK_BUFLEN_IN_RANGE (len ) \
617604 do { \
618605 if ((len) > Buffer::kMaxLength ) { \
619- *error = SB_BUFFER_SIZE_EXCEEDED_ERROR ; \
606+ *error = node::ERR_BUFFER_TOO_LARGE (isolate); \
620607 return MaybeLocal<Value>(); \
621608 } \
622609 } while (0 )
@@ -639,9 +626,13 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
639626 switch (encoding) {
640627 case BUFFER :
641628 {
629+ if (buflen > node::Buffer::kMaxLength ) {
630+ *error = node::ERR_BUFFER_TOO_LARGE (isolate);
631+ return MaybeLocal<Value>();
632+ }
642633 auto maybe_buf = Buffer::Copy (isolate, buf, buflen);
643634 if (maybe_buf.IsEmpty ()) {
644- *error = SB_BUFFER_CREATION_ERROR ;
635+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
645636 return MaybeLocal<Value>();
646637 }
647638 return maybe_buf.ToLocalChecked ();
@@ -651,7 +642,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
651642 if (contains_non_ascii (buf, buflen)) {
652643 char * out = node::UncheckedMalloc (buflen);
653644 if (out == nullptr ) {
654- *error = SB_MALLOC_FAILED_ERROR ;
645+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
655646 return MaybeLocal<Value>();
656647 }
657648 force_ascii (buf, out, buflen);
@@ -666,7 +657,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
666657 v8::NewStringType::kNormal ,
667658 buflen);
668659 if (val.IsEmpty ()) {
669- *error = SB_STRING_TOO_LONG_ERROR ;
660+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
670661 return MaybeLocal<Value>();
671662 }
672663 return val.ToLocalChecked ();
@@ -678,7 +669,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
678669 size_t dlen = base64_encoded_size (buflen);
679670 char * dst = node::UncheckedMalloc (dlen);
680671 if (dst == nullptr ) {
681- *error = SB_MALLOC_FAILED_ERROR ;
672+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
682673 return MaybeLocal<Value>();
683674 }
684675
@@ -692,7 +683,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
692683 size_t dlen = buflen * 2 ;
693684 char * dst = node::UncheckedMalloc (dlen);
694685 if (dst == nullptr ) {
695- *error = SB_MALLOC_FAILED_ERROR ;
686+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
696687 return MaybeLocal<Value>();
697688 }
698689 size_t written = hex_encode (buf, buflen, dst, dlen);
@@ -723,7 +714,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
723714 if (IsBigEndian ()) {
724715 uint16_t * dst = node::UncheckedMalloc<uint16_t >(buflen);
725716 if (dst == nullptr ) {
726- *error = SB_MALLOC_FAILED_ERROR ;
717+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
727718 return MaybeLocal<Value>();
728719 }
729720 size_t nbytes = buflen * sizeof (uint16_t );
0 commit comments