diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-04-02 10:39:26 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-04-07 17:15:13 +0800 |
commit | 63eb267c34198c4bece62cb6432bb2bceb399de6 (patch) | |
tree | 7a3188e205b7205a7cfb2854bd289f5d7fec35d1 /src | |
parent | 289d152ce0222d3be32bf20ef7dc7d8d5e40f218 (diff) | |
download | android-node-v8-63eb267c34198c4bece62cb6432bb2bceb399de6.tar.gz android-node-v8-63eb267c34198c4bece62cb6432bb2bceb399de6.tar.bz2 android-node-v8-63eb267c34198c4bece62cb6432bb2bceb399de6.zip |
src: migrate string_bytes.cc to throw errors with code
PR-URL: https://github.com/nodejs/node/pull/19739
Fixes: https://github.com/nodejs/node/issues/3175
Fixes: https://github.com/nodejs/node/issues/9489
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/string_bytes.cc | 41 |
1 files changed, 16 insertions, 25 deletions
diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 624eb92b93..bbd381d33a 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -23,6 +23,7 @@ #include "base64.h" #include "node_internals.h" +#include "node_errors.h" #include "node_buffer.h" #include <limits.h> @@ -36,20 +37,6 @@ // use external string resources. #define EXTERN_APEX 0xFBEE9 -// TODO(addaleax): These should all have better error messages. In particular, -// they should mention what the actual limits are. -#define SB_MALLOC_FAILED_ERROR \ - v8::Exception::Error(OneByteString(isolate, "\"toString()\" failed")) - -#define SB_STRING_TOO_LONG_ERROR \ - v8::Exception::Error(OneByteString(isolate, "\"toString()\" failed")) - -#define SB_BUFFER_CREATION_ERROR \ - v8::Exception::Error(OneByteString(isolate, "\"toString()\" failed")) - -#define SB_BUFFER_SIZE_EXCEEDED_ERROR \ - v8::Exception::Error(OneByteString(isolate, "\"toString()\" failed")) - namespace node { using v8::HandleScope; @@ -93,7 +80,7 @@ class ExternString: public ResourceType { TypeName* new_data = node::UncheckedMalloc<TypeName>(length); if (new_data == nullptr) { - *error = SB_MALLOC_FAILED_ERROR; + *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); return MaybeLocal<Value>(); } memcpy(new_data, data, length * sizeof(*new_data)); @@ -126,7 +113,7 @@ class ExternString: public ResourceType { if (str.IsEmpty()) { delete h_str; - *error = SB_STRING_TOO_LONG_ERROR; + *error = node::ERR_STRING_TOO_LARGE(isolate); return MaybeLocal<Value>(); } @@ -183,7 +170,7 @@ MaybeLocal<Value> ExternOneByteString::NewSimpleFromCopy(Isolate* isolate, v8::NewStringType::kNormal, length); if (str.IsEmpty()) { - *error = SB_STRING_TOO_LONG_ERROR; + *error = node::ERR_STRING_TOO_LARGE(isolate); return MaybeLocal<Value>(); } return str.ToLocalChecked(); @@ -201,7 +188,7 @@ MaybeLocal<Value> ExternTwoByteString::NewSimpleFromCopy(Isolate* isolate, v8::NewStringType::kNormal, length); if (str.IsEmpty()) { - *error = SB_STRING_TOO_LONG_ERROR; + *error = node::ERR_STRING_TOO_LARGE(isolate); return MaybeLocal<Value>(); } return str.ToLocalChecked(); @@ -616,7 +603,7 @@ static size_t hex_encode(const char* src, size_t slen, char* dst, size_t dlen) { #define CHECK_BUFLEN_IN_RANGE(len) \ do { \ if ((len) > Buffer::kMaxLength) { \ - *error = SB_BUFFER_SIZE_EXCEEDED_ERROR; \ + *error = node::ERR_BUFFER_TOO_LARGE(isolate); \ return MaybeLocal<Value>(); \ } \ } while (0) @@ -639,9 +626,13 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, switch (encoding) { case BUFFER: { + if (buflen > node::Buffer::kMaxLength) { + *error = node::ERR_BUFFER_TOO_LARGE(isolate); + return MaybeLocal<Value>(); + } auto maybe_buf = Buffer::Copy(isolate, buf, buflen); if (maybe_buf.IsEmpty()) { - *error = SB_BUFFER_CREATION_ERROR; + *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); return MaybeLocal<Value>(); } return maybe_buf.ToLocalChecked(); @@ -651,7 +642,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, if (contains_non_ascii(buf, buflen)) { char* out = node::UncheckedMalloc(buflen); if (out == nullptr) { - *error = SB_MALLOC_FAILED_ERROR; + *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); return MaybeLocal<Value>(); } force_ascii(buf, out, buflen); @@ -666,7 +657,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, v8::NewStringType::kNormal, buflen); if (val.IsEmpty()) { - *error = SB_STRING_TOO_LONG_ERROR; + *error = node::ERR_STRING_TOO_LARGE(isolate); return MaybeLocal<Value>(); } return val.ToLocalChecked(); @@ -678,7 +669,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, size_t dlen = base64_encoded_size(buflen); char* dst = node::UncheckedMalloc(dlen); if (dst == nullptr) { - *error = SB_MALLOC_FAILED_ERROR; + *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); return MaybeLocal<Value>(); } @@ -692,7 +683,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, size_t dlen = buflen * 2; char* dst = node::UncheckedMalloc(dlen); if (dst == nullptr) { - *error = SB_MALLOC_FAILED_ERROR; + *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); return MaybeLocal<Value>(); } size_t written = hex_encode(buf, buflen, dst, dlen); @@ -723,7 +714,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, if (IsBigEndian()) { uint16_t* dst = node::UncheckedMalloc<uint16_t>(buflen); if (dst == nullptr) { - *error = SB_MALLOC_FAILED_ERROR; + *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); return MaybeLocal<Value>(); } size_t nbytes = buflen * sizeof(uint16_t); |