GeezerVison is showing github.com — src: do proper StringBytes error handling · inre/node@d56a7e6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +19-23Lines changed: 19 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -567,34 +567,30 @@ Buffer.prototype.copy = function(target, targetStart, sourceStart, sourceEnd) { | 567 | 567 | // This behaves neither like String nor Uint8Array in that we set start/end | 568 | 568 | // to their upper/lower bounds if the value passed is out of range. | 569 | 569 | Buffer.prototype.toString = function(encoding, start, end) { | 570 | | - var result; | 571 | 570 | if (arguments.length === 0) { | 572 | | - result = this.utf8Slice(0, this.length); | 573 | | - } else { | 574 | | - const len = this.length; | 575 | | - if (len === 0) | 576 | | - return ''; | | 571 | + return this.utf8Slice(0, this.length); | | 572 | + } | 577 | 573 |
| 578 | | - if (!start || start < 0) | 579 | | - start = 0; | 580 | | - else if (start >= len) | 581 | | - return ''; | | 574 | + const len = this.length; | | 575 | + if (len === 0) | | 576 | + return ''; | 582 | 577 |
| 583 | | - if (end === undefined || end > len) | 584 | | - end = len; | 585 | | - else if (end <= 0) | 586 | | - return ''; | | 578 | + if (!start || start < 0) | | 579 | + start = 0; | | 580 | + else if (start >= len) | | 581 | + return ''; | 587 | 582 |
| 588 | | - start |= 0; | 589 | | - end |= 0; | | 583 | + if (end === undefined || end > len) | | 584 | + end = len; | | 585 | + else if (end <= 0) | | 586 | + return ''; | 590 | 587 |
| 591 | | - if (end <= start) | 592 | | - return ''; | 593 | | - result = stringSlice(this, encoding, start, end); | 594 | | - } | 595 | | - if (result === undefined) | 596 | | - throw new Error('"toString()" failed'); | 597 | | - return result; | | 588 | + start |= 0; | | 589 | + end |= 0; | | 590 | + | | 591 | + if (end <= start) | | 592 | + return ''; | | 593 | + return stringSlice(this, encoding, start, end); | 598 | 594 | }; | 599 | 595 |
| 600 | 596 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/fs_event_wrap.cc +9-5Lines changed: 9 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,6 +39,7 @@ using v8::FunctionTemplate; | 39 | 39 | using v8::HandleScope; | 40 | 40 | using v8::Integer; | 41 | 41 | using v8::Local; | | 42 | +using v8::MaybeLocal; | 42 | 43 | using v8::Object; | 43 | 44 | using v8::String; | 44 | 45 | using v8::Value; | @@ -187,17 +188,20 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, | 187 | 188 | }; | 188 | 189 |
| 189 | 190 | if (filename != nullptr) { | 190 | | - Local<Value> fn = StringBytes::Encode(env->isolate(), | 191 | | - filename, | 192 | | - wrap->encoding_); | | 191 | + Local<Value> error; | | 192 | + MaybeLocal<Value> fn = StringBytes::Encode(env->isolate(), | | 193 | + filename, | | 194 | + wrap->encoding_, | | 195 | + &error); | 193 | 196 | if (fn.IsEmpty()) { | 194 | 197 | argv[0] = Integer::New(env->isolate(), UV_EINVAL); | 195 | 198 | argv[2] = StringBytes::Encode(env->isolate(), | 196 | 199 | filename, | 197 | 200 | strlen(filename), | 198 | | - BUFFER); | | 201 | + BUFFER, | | 202 | + &error).ToLocalChecked(); | 199 | 203 | } else { | 200 | | - argv[2] = fn; | | 204 | + argv[2] = fn.ToLocalChecked(); | 201 | 205 | } | 202 | 206 | } | 203 | 207 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1570,11 +1570,15 @@ Local<Value> Encode(Isolate* isolate, | 1570 | 1570 | size_t len, | 1571 | 1571 | enum encoding encoding) { | 1572 | 1572 | CHECK_NE(encoding, UCS2); | 1573 | | - return StringBytes::Encode(isolate, buf, len, encoding); | | 1573 | + Local<Value> error; | | 1574 | + return StringBytes::Encode(isolate, buf, len, encoding, &error) | | 1575 | + .ToLocalChecked(); | 1574 | 1576 | } | 1575 | 1577 |
| 1576 | 1578 | Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) { | 1577 | | - return StringBytes::Encode(isolate, buf, len); | | 1579 | + Local<Value> error; | | 1580 | + return StringBytes::Encode(isolate, buf, len, &error) | | 1581 | + .ToLocalChecked(); | 1578 | 1582 | } | 1579 | 1583 |
| 1580 | 1584 | // Returns -1 if the handle was not valid for decoding | |
Collapse file Copy file name to clipboardExpand all lines: src/node_buffer.cc +28-4Lines changed: 28 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -465,14 +465,26 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) { | 465 | 465 |
| 466 | 466 | SLICE_START_END(args[0], args[1], ts_obj_length) | 467 | 467 |
| 468 | | - args.GetReturnValue().Set( | 469 | | - StringBytes::Encode(isolate, ts_obj_data + start, length, encoding)); | | 468 | + Local<Value> error; | | 469 | + MaybeLocal<Value> ret = | | 470 | + StringBytes::Encode(isolate, | | 471 | + ts_obj_data + start, | | 472 | + length, | | 473 | + encoding, | | 474 | + &error); | | 475 | + if (ret.IsEmpty()) { | | 476 | + CHECK(!error.IsEmpty()); | | 477 | + isolate->ThrowException(error); | | 478 | + return; | | 479 | + } | | 480 | + args.GetReturnValue().Set(ret.ToLocalChecked()); | 470 | 481 | } | 471 | 482 |
| 472 | 483 |
| 473 | 484 | template <> | 474 | 485 | void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) { | 475 | | - Environment* env = Environment::GetCurrent(args); | | 486 | + Isolate* isolate = args.GetIsolate(); | | 487 | + Environment* env = Environment::GetCurrent(isolate); | 476 | 488 |
| 477 | 489 | THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); | 478 | 490 | SPREAD_BUFFER_ARG(args.This(), ts_obj); | @@ -509,10 +521,22 @@ void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) { | 509 | 521 | buf = reinterpret_cast<const uint16_t*>(data); | 510 | 522 | } | 511 | 523 |
| 512 | | - args.GetReturnValue().Set(StringBytes::Encode(env->isolate(), buf, length)); | | 524 | + Local<Value> error; | | 525 | + MaybeLocal<Value> ret = | | 526 | + StringBytes::Encode(isolate, | | 527 | + buf, | | 528 | + length, | | 529 | + &error); | 513 | 530 |
| 514 | 531 | if (release) | 515 | 532 | delete[] buf; | | 533 | + | | 534 | + if (ret.IsEmpty()) { | | 535 | + CHECK(!error.IsEmpty()); | | 536 | + isolate->ThrowException(error); | | 537 | + return; | | 538 | + } | | 539 | + args.GetReturnValue().Set(ret.ToLocalChecked()); | 516 | 540 | } | 517 | 541 |
| 518 | 542 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_crypto.cc +27-10Lines changed: 27 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -104,6 +104,7 @@ using v8::Integer; | 104 | 104 | using v8::Isolate; | 105 | 105 | using v8::Local; | 106 | 106 | using v8::Maybe; | | 107 | +using v8::MaybeLocal; | 107 | 108 | using v8::Null; | 108 | 109 | using v8::Object; | 109 | 110 | using v8::Persistent; | @@ -3811,12 +3812,20 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) { | 3811 | 3812 | md_len = 0; | 3812 | 3813 | } | 3813 | 3814 |
| 3814 | | - Local<Value> rc = StringBytes::Encode(env->isolate(), | 3815 | | - reinterpret_cast<const char*>(md_value), | 3816 | | - md_len, | 3817 | | - encoding); | | 3815 | + Local<Value> error; | | 3816 | + MaybeLocal<Value> rc = | | 3817 | + StringBytes::Encode(env->isolate(), | | 3818 | + reinterpret_cast<const char*>(md_value), | | 3819 | + md_len, | | 3820 | + encoding, | | 3821 | + &error); | 3818 | 3822 | delete[] md_value; | 3819 | | - args.GetReturnValue().Set(rc); | | 3823 | + if (rc.IsEmpty()) { | | 3824 | + CHECK(!error.IsEmpty()); | | 3825 | + env->isolate()->ThrowException(error); | | 3826 | + return; | | 3827 | + } | | 3828 | + args.GetReturnValue().Set(rc.ToLocalChecked()); | 3820 | 3829 | } | 3821 | 3830 |
| 3822 | 3831 |
| @@ -3936,11 +3945,19 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) { | 3936 | 3945 | EVP_MD_CTX_cleanup(&hash->mdctx_); | 3937 | 3946 | hash->finalized_ = true; | 3938 | 3947 |
| 3939 | | - Local<Value> rc = StringBytes::Encode(env->isolate(), | 3940 | | - reinterpret_cast<const char*>(md_value), | 3941 | | - md_len, | 3942 | | - encoding); | 3943 | | - args.GetReturnValue().Set(rc); | | 3948 | + Local<Value> error; | | 3949 | + MaybeLocal<Value> rc = | | 3950 | + StringBytes::Encode(env->isolate(), | | 3951 | + reinterpret_cast<const char*>(md_value), | | 3952 | + md_len, | | 3953 | + encoding, | | 3954 | + &error); | | 3955 | + if (rc.IsEmpty()) { | | 3956 | + CHECK(!error.IsEmpty()); | | 3957 | + env->isolate()->ThrowException(error); | | 3958 | + return; | | 3959 | + } | | 3960 | + args.GetReturnValue().Set(rc.ToLocalChecked()); | 3944 | 3961 | } | 3945 | 3962 |
| 3946 | 3963 |
| |
|