@@ -730,15 +730,37 @@ void ReadDoubleBE(const FunctionCallbackInfo<Value>& args) {
730730
731731
732732template <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 );
735753
736- T val = args[1 ]->NumberValue ();
737- uint32_t offset = args[2 ]->Uint32Value ();
738754 size_t memcpy_num = sizeof (T);
739755 if (offset + sizeof (T) > ts_obj_length)
740756 memcpy_num = ts_obj_length - offset;
741757
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+
742764 union NoAlias {
743765 T val;
744766 char bytes[sizeof (T)];
@@ -749,31 +771,26 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
749771 if (endianness != GetEndianness ())
750772 Swizzle (na.bytes , sizeof (na.bytes ));
751773 memcpy (ptr, na.bytes , memcpy_num);
752- return offset + memcpy_num;
753774}
754775
755776
756777void 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);
759779}
760780
761781
762782void 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);
765784}
766785
767786
768787void 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);
771789}
772790
773791
774792void 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);
777794}
778795
779796
0 commit comments