@@ -24,8 +24,8 @@ rb_dl_scan_callback_args(long stack[], const char *proto,
24
24
VALUE val ;
25
25
26
26
sp = stack ;
27
- for ( i = 1 ; proto [i ]; i ++ ) {
28
- switch ( proto [i ] ) {
27
+ for ( i = 1 ; proto [i ]; i ++ ) {
28
+ switch ( proto [i ]) {
29
29
case 'C' :
30
30
{
31
31
char v ;
@@ -162,11 +162,11 @@ dlsizeof(const char *cstr)
162
162
163
163
len = strlen (cstr );
164
164
size = 0 ;
165
- for ( i = 0 ; i < len ; i ++ ) {
165
+ for ( i = 0 ; i < len ; i ++ ) {
166
166
n = 1 ;
167
- if ( isdigit (cstr [i + 1 ]) ) {
167
+ if ( isdigit (cstr [i + 1 ])) {
168
168
dlen = 1 ;
169
- while ( isdigit (cstr [i + dlen ]) ) { dlen ++ ; };
169
+ while ( isdigit (cstr [i + dlen ])) { dlen ++ ; };
170
170
dlen -- ;
171
171
d = ALLOCA_N (char , dlen + 1 );
172
172
strncpy (d , cstr + i + 1 , dlen );
@@ -177,7 +177,7 @@ dlsizeof(const char *cstr)
177
177
dlen = 0 ;
178
178
}
179
179
180
- switch ( cstr [i ] ) {
180
+ switch ( cstr [i ]) {
181
181
case 'I' :
182
182
DLALIGN (0 ,size ,INT_ALIGN );
183
183
case 'i' :
@@ -234,9 +234,9 @@ c_farray(VALUE v, long *size)
234
234
len = RARRAY (v )-> len ;
235
235
* size = sizeof (float ) * len ;
236
236
ary = dlmalloc (* size );
237
- for ( i = 0 ; i < len ; i ++ ) {
237
+ for ( i = 0 ; i < len ; i ++ ) {
238
238
e = rb_ary_entry (v , i );
239
- switch ( TYPE (e ) ) {
239
+ switch ( TYPE (e )) {
240
240
case T_FLOAT :
241
241
ary [i ] = (float )(RFLOAT (e )-> value );
242
242
break ;
@@ -262,9 +262,9 @@ c_darray(VALUE v, long *size)
262
262
len = RARRAY (v )-> len ;
263
263
* size = sizeof (double ) * len ;
264
264
ary = dlmalloc (* size );
265
- for ( i = 0 ; i < len ; i ++ ) {
265
+ for ( i = 0 ; i < len ; i ++ ) {
266
266
e = rb_ary_entry (v , i );
267
- switch ( TYPE (e ) ) {
267
+ switch ( TYPE (e )) {
268
268
case T_FLOAT :
269
269
ary [i ] = (double )(RFLOAT (e )-> value );
270
270
break ;
@@ -290,9 +290,9 @@ c_larray(VALUE v, long *size)
290
290
len = RARRAY (v )-> len ;
291
291
* size = sizeof (long ) * len ;
292
292
ary = dlmalloc (* size );
293
- for ( i = 0 ; i < len ; i ++ ) {
293
+ for ( i = 0 ; i < len ; i ++ ) {
294
294
e = rb_ary_entry (v , i );
295
- switch ( TYPE (e ) ) {
295
+ switch ( TYPE (e )) {
296
296
case T_FIXNUM :
297
297
case T_BIGNUM :
298
298
ary [i ] = (long )(NUM2INT (e ));
@@ -319,9 +319,9 @@ c_iarray(VALUE v, long *size)
319
319
len = RARRAY (v )-> len ;
320
320
* size = sizeof (int ) * len ;
321
321
ary = dlmalloc (* size );
322
- for ( i = 0 ; i < len ; i ++ ) {
322
+ for ( i = 0 ; i < len ; i ++ ) {
323
323
e = rb_ary_entry (v , i );
324
- switch ( TYPE (e ) ) {
324
+ switch ( TYPE (e )) {
325
325
case T_FIXNUM :
326
326
case T_BIGNUM :
327
327
ary [i ] = (int )(NUM2INT (e ));
@@ -348,9 +348,9 @@ c_harray(VALUE v, long *size)
348
348
len = RARRAY (v )-> len ;
349
349
* size = sizeof (short ) * len ;
350
350
ary = dlmalloc (* size );
351
- for ( i = 0 ; i < len ; i ++ ) {
351
+ for ( i = 0 ; i < len ; i ++ ) {
352
352
e = rb_ary_entry (v , i );
353
- switch ( TYPE (e ) ) {
353
+ switch ( TYPE (e )) {
354
354
case T_FIXNUM :
355
355
case T_BIGNUM :
356
356
ary [i ] = (short )(NUM2INT (e ));
@@ -377,9 +377,9 @@ c_carray(VALUE v, long *size)
377
377
len = RARRAY (v )-> len ;
378
378
* size = sizeof (char ) * len ;
379
379
ary = dlmalloc (* size );
380
- for ( i = 0 ; i < len ; i ++ ) {
380
+ for ( i = 0 ; i < len ; i ++ ) {
381
381
e = rb_ary_entry (v , i );
382
- switch ( TYPE (e ) ) {
382
+ switch ( TYPE (e )) {
383
383
case T_FIXNUM :
384
384
case T_BIGNUM :
385
385
ary [i ] = (char )(NUM2INT (e ));
@@ -401,15 +401,23 @@ c_parray(VALUE v, long *size)
401
401
{
402
402
int i , len ;
403
403
void * * ary ;
404
- VALUE e ;
404
+ VALUE e , tmp ;
405
405
406
406
len = RARRAY (v )-> len ;
407
407
* size = sizeof (void * ) * len ;
408
408
ary = dlmalloc (* size );
409
- for ( i = 0 ; i < len ; i ++ ) {
409
+ for ( i = 0 ; i < len ; i ++ ) {
410
410
e = rb_ary_entry (v , i );
411
- switch ( TYPE (e ) ){
411
+ switch (TYPE (e )) {
412
+ default :
413
+ tmp = rb_check_string_type (e );
414
+ if (NIL_P (tmp )) {
415
+ rb_raise (rb_eDLTypeError , "unexpected type of the element #%d" , i );
416
+ }
417
+ e = tmp ;
418
+ /* fall through */
412
419
case T_STRING :
420
+ rb_check_safe_str (e );
413
421
{
414
422
char * str , * src ;
415
423
src = RSTRING (e )-> ptr ;
@@ -421,7 +429,7 @@ c_parray(VALUE v, long *size)
421
429
ary [i ] = NULL ;
422
430
break ;
423
431
case T_DATA :
424
- if ( rb_obj_is_kind_of (e , rb_cDLPtrData ) ) {
432
+ if ( rb_obj_is_kind_of (e , rb_cDLPtrData )) {
425
433
struct ptr_data * pdata ;
426
434
Data_Get_Struct (e , struct ptr_data , pdata );
427
435
ary [i ] = (void * )(pdata -> ptr );
@@ -430,9 +438,6 @@ c_parray(VALUE v, long *size)
430
438
rb_raise (rb_eDLTypeError , "unexpected type of the element #%d" , i );
431
439
}
432
440
break ;
433
- default :
434
- rb_raise (rb_eDLTypeError , "unexpected type of the element #%d" , i );
435
- break ;
436
441
}
437
442
}
438
443
@@ -445,24 +450,26 @@ rb_ary2cary(char t, VALUE v, long *size)
445
450
int len ;
446
451
VALUE val0 ;
447
452
448
- if ( TYPE (v ) != T_ARRAY ){
453
+ val0 = rb_check_array_type (v );
454
+ if (NIL_P (TYPE (val0 ))) {
449
455
rb_raise (rb_eDLTypeError , "an array is expected." );
450
456
}
457
+ v = val0 ;
451
458
452
459
len = RARRAY (v )-> len ;
453
- if ( len == 0 ) {
460
+ if ( len == 0 ) {
454
461
return NULL ;
455
462
}
456
463
457
- if ( !size ) {
464
+ if ( !size ) {
458
465
size = ALLOCA_N (long ,1 );
459
466
}
460
467
461
468
val0 = rb_ary_entry (v ,0 );
462
- switch ( TYPE (val0 ) ) {
469
+ switch ( TYPE (val0 )) {
463
470
case T_FIXNUM :
464
471
case T_BIGNUM :
465
- switch ( t ) {
472
+ switch ( t ) {
466
473
case 'C' : case 'c' :
467
474
return (void * )c_carray (v ,size );
468
475
case 'H' : case 'h' :
@@ -477,15 +484,15 @@ rb_ary2cary(char t, VALUE v, long *size)
477
484
case T_STRING :
478
485
return (void * )c_parray (v ,size );
479
486
case T_FLOAT :
480
- switch ( t ) {
487
+ switch ( t ) {
481
488
case 'F' : case 'f' :
482
489
return (void * )c_farray (v ,size );
483
490
case 'D' : case 'd' : case 0 :
484
491
return (void * )c_darray (v ,size );
485
492
}
486
493
rb_raise (rb_eDLTypeError , "type mismatch" );
487
494
case T_DATA :
488
- if ( rb_obj_is_kind_of (val0 , rb_cDLPtrData ) ) {
495
+ if ( rb_obj_is_kind_of (val0 , rb_cDLPtrData )) {
489
496
return (void * )c_parray (v ,size );
490
497
}
491
498
rb_raise (rb_eDLTypeError , "type mismatch" );
@@ -516,7 +523,7 @@ rb_ary_to_ptr(int argc, VALUE argv[], VALUE self)
516
523
VALUE t ;
517
524
long size ;
518
525
519
- switch ( rb_scan_args (argc , argv , "01" , & t ) ) {
526
+ switch ( rb_scan_args (argc , argv , "01" , & t )) {
520
527
case 1 :
521
528
ptr = rb_ary2cary (StringValuePtr (t )[0 ], self , & size );
522
529
break ;
@@ -556,15 +563,13 @@ rb_dl_malloc(VALUE self, VALUE size)
556
563
VALUE
557
564
rb_dl_strdup (VALUE self , VALUE str )
558
565
{
559
- rb_secure (4 );
560
- str = rb_String (str );
566
+ SafeStringValue (str );
561
567
return rb_dlptr_new (strdup (RSTRING (str )-> ptr ), RSTRING (str )-> len , dlfree );
562
568
}
563
569
564
570
static VALUE
565
571
rb_dl_sizeof (VALUE self , VALUE str )
566
572
{
567
- rb_secure (4 );
568
573
return INT2NUM (dlsizeof (StringValuePtr (str )));
569
574
}
570
575
@@ -577,9 +582,9 @@ rb_dl_callback(int argc, VALUE argv[], VALUE self)
577
582
578
583
rb_secure (4 );
579
584
proc = Qnil ;
580
- switch ( rb_scan_args (argc , argv , "11" , & type , & proc ) ) {
585
+ switch ( rb_scan_args (argc , argv , "11" , & type , & proc )) {
581
586
case 1 :
582
- if ( rb_block_given_p () ) {
587
+ if ( rb_block_given_p ()) {
583
588
proc = rb_f_lambda ();
584
589
}
585
590
else {
@@ -589,8 +594,8 @@ rb_dl_callback(int argc, VALUE argv[], VALUE self)
589
594
break ;
590
595
}
591
596
592
- Check_Type (type , T_STRING );
593
- switch ( RSTRING (type )-> ptr [0 ] ) {
597
+ StringValue (type );
598
+ switch ( RSTRING (type )-> ptr [0 ]) {
594
599
case '0' :
595
600
rettype = 0x00 ;
596
601
break ;
@@ -620,13 +625,13 @@ rb_dl_callback(int argc, VALUE argv[], VALUE self)
620
625
}
621
626
622
627
entry = -1 ;
623
- for ( i = 0 ; i < MAX_CALLBACK ; i ++ ) {
624
- if ( rb_hash_aref (DLFuncTable , rb_assoc_new (INT2NUM (rettype ), INT2NUM (i ))) == Qnil ) {
628
+ for ( i = 0 ; i < MAX_CALLBACK ; i ++ ) {
629
+ if ( rb_hash_aref (DLFuncTable , rb_assoc_new (INT2NUM (rettype ), INT2NUM (i ))) == Qnil ) {
625
630
entry = i ;
626
631
break ;
627
632
}
628
633
}
629
- if ( entry < 0 ) {
634
+ if ( entry < 0 ) {
630
635
rb_raise (rb_eDLError , "too many callbacks are defined." );
631
636
}
632
637
@@ -646,9 +651,9 @@ rb_dl_remove_callback(VALUE mod, VALUE sym)
646
651
647
652
rb_secure (4 );
648
653
f = rb_dlsym2csym (sym );
649
- for ( i = 0 ; i < CALLBACK_TYPES ; i ++ ) {
650
- for ( j = 0 ; j < MAX_CALLBACK ; j ++ ) {
651
- if ( rb_dl_callback_table [i ][j ] == f ) {
654
+ for ( i = 0 ; i < CALLBACK_TYPES ; i ++ ) {
655
+ for ( j = 0 ; j < MAX_CALLBACK ; j ++ ) {
656
+ if ( rb_dl_callback_table [i ][j ] == f ) {
652
657
rb_hash_aset (DLFuncTable , rb_assoc_new (INT2NUM (i ),INT2NUM (j )),Qnil );
653
658
break ;
654
659
}
0 commit comments