@@ -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
0 commit comments