23
23
24
24
#include " base64.h"
25
25
#include " node_internals.h"
26
+ #include " node_errors.h"
26
27
#include " node_buffer.h"
27
28
28
29
#include < limits.h>
36
37
// use external string resources.
37
38
#define EXTERN_APEX 0xFBEE9
38
39
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
-
53
40
namespace node {
54
41
55
42
using v8::HandleScope;
@@ -93,7 +80,7 @@ class ExternString: public ResourceType {
93
80
94
81
TypeName* new_data = node::UncheckedMalloc<TypeName>(length);
95
82
if (new_data == nullptr ) {
96
- *error = SB_MALLOC_FAILED_ERROR ;
83
+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
97
84
return MaybeLocal<Value>();
98
85
}
99
86
memcpy (new_data, data, length * sizeof (*new_data));
@@ -126,7 +113,7 @@ class ExternString: public ResourceType {
126
113
127
114
if (str.IsEmpty ()) {
128
115
delete h_str;
129
- *error = SB_STRING_TOO_LONG_ERROR ;
116
+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
130
117
return MaybeLocal<Value>();
131
118
}
132
119
@@ -183,7 +170,7 @@ MaybeLocal<Value> ExternOneByteString::NewSimpleFromCopy(Isolate* isolate,
183
170
v8::NewStringType::kNormal ,
184
171
length);
185
172
if (str.IsEmpty ()) {
186
- *error = SB_STRING_TOO_LONG_ERROR ;
173
+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
187
174
return MaybeLocal<Value>();
188
175
}
189
176
return str.ToLocalChecked ();
@@ -201,7 +188,7 @@ MaybeLocal<Value> ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate,
201
188
v8::NewStringType::kNormal ,
202
189
length);
203
190
if (str.IsEmpty ()) {
204
- *error = SB_STRING_TOO_LONG_ERROR ;
191
+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
205
192
return MaybeLocal<Value>();
206
193
}
207
194
return str.ToLocalChecked ();
@@ -616,7 +603,7 @@ static size_t hex_encode(const char* src, size_t slen, char* dst, size_t dlen) {
616
603
#define CHECK_BUFLEN_IN_RANGE (len ) \
617
604
do { \
618
605
if ((len) > Buffer::kMaxLength ) { \
619
- *error = SB_BUFFER_SIZE_EXCEEDED_ERROR; \
606
+ *error = node::ERR_BUFFER_TOO_LARGE (isolate); \
620
607
return MaybeLocal<Value>(); \
621
608
} \
622
609
} while (0 )
@@ -639,9 +626,13 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
639
626
switch (encoding) {
640
627
case BUFFER:
641
628
{
629
+ if (buflen > node::Buffer::kMaxLength ) {
630
+ *error = node::ERR_BUFFER_TOO_LARGE (isolate);
631
+ return MaybeLocal<Value>();
632
+ }
642
633
auto maybe_buf = Buffer::Copy (isolate, buf, buflen);
643
634
if (maybe_buf.IsEmpty ()) {
644
- *error = SB_BUFFER_CREATION_ERROR ;
635
+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
645
636
return MaybeLocal<Value>();
646
637
}
647
638
return maybe_buf.ToLocalChecked ();
@@ -651,7 +642,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
651
642
if (contains_non_ascii (buf, buflen)) {
652
643
char * out = node::UncheckedMalloc (buflen);
653
644
if (out == nullptr ) {
654
- *error = SB_MALLOC_FAILED_ERROR ;
645
+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
655
646
return MaybeLocal<Value>();
656
647
}
657
648
force_ascii (buf, out, buflen);
@@ -666,7 +657,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
666
657
v8::NewStringType::kNormal ,
667
658
buflen);
668
659
if (val.IsEmpty ()) {
669
- *error = SB_STRING_TOO_LONG_ERROR ;
660
+ *error = node::ERR_STRING_TOO_LARGE (isolate) ;
670
661
return MaybeLocal<Value>();
671
662
}
672
663
return val.ToLocalChecked ();
@@ -678,7 +669,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
678
669
size_t dlen = base64_encoded_size (buflen);
679
670
char * dst = node::UncheckedMalloc (dlen);
680
671
if (dst == nullptr ) {
681
- *error = SB_MALLOC_FAILED_ERROR ;
672
+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
682
673
return MaybeLocal<Value>();
683
674
}
684
675
@@ -692,7 +683,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
692
683
size_t dlen = buflen * 2 ;
693
684
char * dst = node::UncheckedMalloc (dlen);
694
685
if (dst == nullptr ) {
695
- *error = SB_MALLOC_FAILED_ERROR ;
686
+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
696
687
return MaybeLocal<Value>();
697
688
}
698
689
size_t written = hex_encode (buf, buflen, dst, dlen);
@@ -723,7 +714,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
723
714
if (IsBigEndian ()) {
724
715
uint16_t * dst = node::UncheckedMalloc<uint16_t >(buflen);
725
716
if (dst == nullptr ) {
726
- *error = SB_MALLOC_FAILED_ERROR ;
717
+ *error = node::ERR_MEMORY_ALLOCATION_FAILED (isolate) ;
727
718
return MaybeLocal<Value>();
728
719
}
729
720
size_t nbytes = buflen * sizeof (uint16_t );
0 commit comments