18
18
if (!(r)) return env->ThrowRangeError (" out of range index" ); \
19
19
} while (0 )
20
20
21
+ #define THROW_AND_RETURN_UNLESS_BUFFER (env, obj ) \
22
+ do { \
23
+ if (!HasInstance (obj)) \
24
+ return env->ThrowTypeError (" argument should be a Buffer" ); \
25
+ } while (0 )
26
+
21
27
#define ARGS_THIS (argT ) \
22
28
Local<Object> obj = argT; \
23
29
size_t obj_length = obj->GetIndexedPropertiesExternalArrayDataLength (); \
@@ -223,7 +229,12 @@ template <encoding encoding>
223
229
void StringSlice (const FunctionCallbackInfo<Value>& args) {
224
230
Environment* env = Environment::GetCurrent (args);
225
231
232
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
226
233
ARGS_THIS (args.This ())
234
+
235
+ if (obj_length == 0 )
236
+ return args.GetReturnValue ().SetEmptyString ();
237
+
227
238
SLICE_START_END (args[0 ], args[1 ], obj_length)
228
239
229
240
args.GetReturnValue ().Set (
@@ -235,7 +246,12 @@ template <>
235
246
void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) {
236
247
Environment* env = Environment::GetCurrent (args);
237
248
249
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
238
250
ARGS_THIS (args.This ())
251
+
252
+ if (obj_length == 0 )
253
+ return args.GetReturnValue ().SetEmptyString ();
254
+
239
255
SLICE_START_END (args[0 ], args[1 ], obj_length)
240
256
length /= 2 ;
241
257
@@ -306,8 +322,9 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
306
322
if (!HasInstance (args[0 ]))
307
323
return env->ThrowTypeError (" first arg should be a Buffer" );
308
324
309
- Local<Object> target = args[0 ]-> ToObject (env-> isolate () );
325
+ Local<Object> target = args[0 ]. As <Object>( );
310
326
327
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
311
328
ARGS_THIS (args.This ())
312
329
size_t target_length = target->GetIndexedPropertiesExternalArrayDataLength ();
313
330
char * target_data = static_cast <char *>(
@@ -340,6 +357,7 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
340
357
341
358
342
359
void Fill (const FunctionCallbackInfo<Value>& args) {
360
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
343
361
ARGS_THIS (args[0 ].As <Object>())
344
362
345
363
size_t start = args[2 ]->Uint32Value ();
@@ -383,6 +401,7 @@ template <encoding encoding>
383
401
void StringWrite (const FunctionCallbackInfo<Value>& args) {
384
402
Environment* env = Environment::GetCurrent (args);
385
403
404
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args.This ());
386
405
ARGS_THIS (args.This ())
387
406
388
407
if (!args[0 ]->IsString ())
@@ -459,6 +478,7 @@ static inline void Swizzle(char* start, unsigned int len) {
459
478
460
479
template <typename T, enum Endianness endianness>
461
480
void ReadFloatGeneric (const FunctionCallbackInfo<Value>& args) {
481
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
462
482
ARGS_THIS (args[0 ].As <Object>());
463
483
464
484
uint32_t offset = args[1 ]->Uint32Value ();
@@ -522,21 +542,25 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
522
542
523
543
524
544
void WriteFloatLE (const FunctionCallbackInfo<Value>& args) {
545
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
525
546
args.GetReturnValue ().Set (WriteFloatGeneric<float , kLittleEndian >(args));
526
547
}
527
548
528
549
529
550
void WriteFloatBE (const FunctionCallbackInfo<Value>& args) {
551
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
530
552
args.GetReturnValue ().Set (WriteFloatGeneric<float , kBigEndian >(args));
531
553
}
532
554
533
555
534
556
void WriteDoubleLE (const FunctionCallbackInfo<Value>& args) {
557
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
535
558
args.GetReturnValue ().Set (WriteFloatGeneric<double , kLittleEndian >(args));
536
559
}
537
560
538
561
539
562
void WriteDoubleBE (const FunctionCallbackInfo<Value>& args) {
563
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
540
564
args.GetReturnValue ().Set (WriteFloatGeneric<double , kBigEndian >(args));
541
565
}
542
566
@@ -550,6 +574,10 @@ void ByteLengthUtf8(const FunctionCallbackInfo<Value> &args) {
550
574
551
575
552
576
void Compare (const FunctionCallbackInfo<Value> &args) {
577
+ Environment* env = Environment::GetCurrent (args);
578
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args[0 ]);
579
+ THROW_AND_RETURN_UNLESS_BUFFER (env, args[1 ]);
580
+
553
581
Local<Object> obj_a = args[0 ].As <Object>();
554
582
char * obj_a_data =
555
583
static_cast <char *>(obj_a->GetIndexedPropertiesExternalArrayData ());
@@ -599,10 +627,10 @@ int32_t IndexOf(const char* haystack,
599
627
600
628
601
629
void IndexOfString (const FunctionCallbackInfo<Value>& args) {
602
- ASSERT (args[0 ]->IsObject ());
603
630
ASSERT (args[1 ]->IsString ());
604
631
ASSERT (args[2 ]->IsNumber ());
605
632
633
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
606
634
ARGS_THIS (args[0 ].As <Object>());
607
635
node::Utf8Value str (args.GetIsolate (), args[1 ]);
608
636
int32_t offset_i32 = args[2 ]->Int32Value ();
@@ -630,10 +658,10 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
630
658
631
659
632
660
void IndexOfBuffer (const FunctionCallbackInfo<Value>& args) {
633
- ASSERT (args[0 ]->IsObject ());
634
661
ASSERT (args[1 ]->IsObject ());
635
662
ASSERT (args[2 ]->IsNumber ());
636
663
664
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
637
665
ARGS_THIS (args[0 ].As <Object>());
638
666
Local<Object> buf = args[1 ].As <Object>();
639
667
int32_t offset_i32 = args[2 ]->Int32Value ();
@@ -667,10 +695,10 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
667
695
668
696
669
697
void IndexOfNumber (const FunctionCallbackInfo<Value>& args) {
670
- ASSERT (args[0 ]->IsObject ());
671
698
ASSERT (args[1 ]->IsNumber ());
672
699
ASSERT (args[2 ]->IsNumber ());
673
700
701
+ THROW_AND_RETURN_UNLESS_BUFFER (Environment::GetCurrent (args), args[0 ]);
674
702
ARGS_THIS (args[0 ].As <Object>());
675
703
uint32_t needle = args[1 ]->Uint32Value ();
676
704
int32_t offset_i32 = args[2 ]->Int32Value ();
0 commit comments