| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f90227b commit fcf0e8e
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1028,20 +1028,13 @@ Buffer.prototype.writeInt32BE = function(value, offset, noAssert) { | |||
| 1028 | 1028 | }; | |
| 1029 | 1029 | ||
| 1030 | 1030 | ||
| 1031 | - function checkFloat(buffer, value, offset, ext) { | ||
| 1032 | - if (!(buffer instanceof Buffer)) | ||
| 1033 | - throw new TypeError('buffer must be a Buffer instance'); | ||
| 1034 | - if (offset + ext > buffer.length) | ||
| 1035 | - throw new RangeError('index out of range'); | ||
| 1036 | - } | ||
| 1037 | - | ||
| 1038 | - | ||
| 1039 | 1031 | Buffer.prototype.writeFloatLE = function writeFloatLE(val, offset, noAssert) { | |
| 1040 | 1032 | val = +val; | |
| 1041 | 1033 | offset = offset >>> 0; | |
| 1042 | 1034 | if (!noAssert) | |
| 1043 | - checkFloat(this, val, offset, 4); | ||
| 1044 | - binding.writeFloatLE(this, val, offset); | ||
| 1035 | + binding.writeFloatLE(this, val, offset); | ||
| 1036 | + else | ||
| 1037 | + binding.writeFloatLE(this, val, offset, true); | ||
| 1045 | 1038 | return offset + 4; | |
| 1046 | 1039 | }; | |
| 1047 | 1040 | ||
@@ -1050,8 +1043,9 @@ Buffer.prototype.writeFloatBE = function writeFloatBE(val, offset, noAssert) { | |||
| 1050 | 1043 | val = +val; | |
| 1051 | 1044 | offset = offset >>> 0; | |
| 1052 | 1045 | if (!noAssert) | |
| 1053 | - checkFloat(this, val, offset, 4); | ||
| 1054 | - binding.writeFloatBE(this, val, offset); | ||
| 1046 | + binding.writeFloatBE(this, val, offset); | ||
| 1047 | + else | ||
| 1048 | + binding.writeFloatBE(this, val, offset, true); | ||
| 1055 | 1049 | return offset + 4; | |
| 1056 | 1050 | }; | |
| 1057 | 1051 | ||
@@ -1060,8 +1054,9 @@ Buffer.prototype.writeDoubleLE = function writeDoubleLE(val, offset, noAssert) { | |||
| 1060 | 1054 | val = +val; | |
| 1061 | 1055 | offset = offset >>> 0; | |
| 1062 | 1056 | if (!noAssert) | |
| 1063 | - checkFloat(this, val, offset, 8); | ||
| 1064 | - binding.writeDoubleLE(this, val, offset); | ||
| 1057 | + binding.writeDoubleLE(this, val, offset); | ||
| 1058 | + else | ||
| 1059 | + binding.writeDoubleLE(this, val, offset, true); | ||
| 1065 | 1060 | return offset + 8; | |
| 1066 | 1061 | }; | |
| 1067 | 1062 | ||
@@ -1070,7 +1065,8 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) { | |||
| 1070 | 1065 | val = +val; | |
| 1071 | 1066 | offset = offset >>> 0; | |
| 1072 | 1067 | if (!noAssert) | |
| 1073 | - checkFloat(this, val, offset, 8); | ||
| 1074 | - binding.writeDoubleBE(this, val, offset); | ||
| 1068 | + binding.writeDoubleBE(this, val, offset); | ||
| 1069 | + else | ||
| 1070 | + binding.writeDoubleBE(this, val, offset, true); | ||
| 1075 | 1071 | return offset + 8; | |
| 1076 | 1072 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -730,15 +730,37 @@ void ReadDoubleBE(const FunctionCallbackInfo<Value>& args) { | |||
| 730 | 730 | ||
| 731 | 731 | ||
| 732 | 732 | template <typename T, enum Endianness endianness> | |
| 733 | - uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) { | ||
| 734 | - SPREAD_ARG(args[0], ts_obj); | ||
| 733 | + void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) { | ||
| 734 | + Environment* env = Environment::GetCurrent(args); | ||
| 735 | + | ||
| 736 | + bool should_assert = args.Length() < 4; | ||
| 737 | + | ||
| 738 | + if (should_assert) { | ||
| 739 | + THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]); | ||
| 740 | + } | ||
| 741 | + | ||
| 742 | + Local<Uint8Array> ts_obj = args[0].As<Uint8Array>(); | ||
| 743 | + ArrayBuffer::Contents ts_obj_c = ts_obj->Buffer()->GetContents(); | ||
| 744 | + const size_t ts_obj_offset = ts_obj->ByteOffset(); | ||
| 745 | + const size_t ts_obj_length = ts_obj->ByteLength(); | ||
| 746 | + char* const ts_obj_data = | ||
| 747 | + static_cast<char*>(ts_obj_c.Data()) + ts_obj_offset; | ||
| 748 | + if (ts_obj_length > 0) | ||
| 749 | + CHECK_NE(ts_obj_data, nullptr); | ||
| 750 | + | ||
| 751 | + T val = args[1]->NumberValue(env->context()).FromMaybe(0); | ||
| 752 | + size_t offset = args[2]->IntegerValue(env->context()).FromMaybe(0); | ||
| 735 | 753 | ||
| 736 | - T val = args[1]->NumberValue(); | ||
| 737 | - uint32_t offset = args[2]->Uint32Value(); | ||
| 738 | 754 | size_t memcpy_num = sizeof(T); | |
| 739 | 755 | if (offset + sizeof(T) > ts_obj_length) | |
| 740 | 756 | memcpy_num = ts_obj_length - offset; | |
| 741 | 757 | ||
| 758 | + if (should_assert) { | ||
| 759 | + CHECK_NOT_OOB(offset + memcpy_num >= memcpy_num); | ||
| 760 | + CHECK_NOT_OOB(offset + memcpy_num <= ts_obj_length); | ||
| 761 | + } | ||
| 762 | + CHECK_LE(offset + memcpy_num, ts_obj_length); | ||
| 763 | + | ||
| 742 | 764 | union NoAlias { | |
| 743 | 765 | T val; | |
| 744 | 766 | char bytes[sizeof(T)]; | |
@@ -749,31 +771,26 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) { | |||
| 749 | 771 | if (endianness != GetEndianness()) | |
| 750 | 772 | Swizzle(na.bytes, sizeof(na.bytes)); | |
| 751 | 773 | memcpy(ptr, na.bytes, memcpy_num); | |
| 752 | - return offset + memcpy_num; | ||
| 753 | 774 | } | |
| 754 | 775 | ||
| 755 | 776 | ||
| 756 | 777 | void WriteFloatLE(const FunctionCallbackInfo<Value>& args) { | |
| 757 | - THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); | ||
| 758 | - args.GetReturnValue().Set(WriteFloatGeneric<float, kLittleEndian>(args)); | ||
| 778 | + WriteFloatGeneric<float, kLittleEndian>(args); | ||
| 759 | 779 | } | |
| 760 | 780 | ||
| 761 | 781 | ||
| 762 | 782 | void WriteFloatBE(const FunctionCallbackInfo<Value>& args) { | |
| 763 | - THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); | ||
| 764 | - args.GetReturnValue().Set(WriteFloatGeneric<float, kBigEndian>(args)); | ||
| 783 | + WriteFloatGeneric<float, kBigEndian>(args); | ||
| 765 | 784 | } | |
| 766 | 785 | ||
| 767 | 786 | ||
| 768 | 787 | void WriteDoubleLE(const FunctionCallbackInfo<Value>& args) { | |
| 769 | - THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); | ||
| 770 | - args.GetReturnValue().Set(WriteFloatGeneric<double, kLittleEndian>(args)); | ||
| 788 | + WriteFloatGeneric<double, kLittleEndian>(args); | ||
| 771 | 789 | } | |
| 772 | 790 | ||
| 773 | 791 | ||
| 774 | 792 | void WriteDoubleBE(const FunctionCallbackInfo<Value>& args) { | |
| 775 | - THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); | ||
| 776 | - args.GetReturnValue().Set(WriteFloatGeneric<double, kBigEndian>(args)); | ||
| 793 | + WriteFloatGeneric<double, kBigEndian>(args); | ||
| 777 | 794 | } | |
| 778 | 795 | ||
| 779 | 796 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments