36
36
37
37
Tcl_Interp * RTcl_interp ;
38
38
39
+ /* For Tcl < 8.7 */
40
+ #ifndef TCL_SIZE_MAX
41
+ typedef int Tcl_Size ;
42
+ #endif
43
+
39
44
static void RTcl_dec_refcount (SEXP R_tclobj )
40
45
{
41
46
Tcl_DecrRefCount ((Tcl_Obj * ) R_ExternalPtrAddr (R_tclobj ));
@@ -331,20 +336,20 @@ SEXP RTcl_StringFromObj(SEXP args)
331
336
332
337
SEXP RTcl_ObjAsCharVector (SEXP args )
333
338
{
334
- int count ;
339
+ Tcl_Size count , i ;
335
340
Tcl_Obj * * elem , * obj ;
336
- int ret , i ;
341
+ int ret ;
337
342
SEXP ans ;
338
343
339
344
if (TYPEOF (CADR (args )) != EXTPTRSXP )
340
345
error (_ ("invalid argument" ));
341
346
obj = (Tcl_Obj * ) R_ExternalPtrAddr (CADR (args ));
342
347
if (!obj ) error (_ ("invalid tclObj -- perhaps saved from another session?" ));
343
348
ret = Tcl_ListObjGetElements (RTcl_interp , obj , & count , & elem );
344
- if (ret != TCL_OK )
349
+ if (ret != TCL_OK || count > R_XLEN_T_MAX )
345
350
return RTcl_StringFromObj (args );
346
-
347
- PROTECT (ans = allocVector (STRSXP , count ));
351
+
352
+ PROTECT (ans = allocVector (STRSXP , ( R_xlen_t ) count ));
348
353
for (i = 0 ; i < count ; i ++ ) {
349
354
char * s ;
350
355
Tcl_DString s_ds ;
@@ -405,9 +410,9 @@ SEXP RTcl_ObjFromCharVector(SEXP args)
405
410
406
411
SEXP RTcl_ObjAsDoubleVector (SEXP args )
407
412
{
408
- int count ;
413
+ Tcl_Size count , i ;
409
414
Tcl_Obj * * elem , * obj ;
410
- int ret , i ;
415
+ int ret ;
411
416
double x ;
412
417
SEXP ans ;
413
418
@@ -422,10 +427,10 @@ SEXP RTcl_ObjAsDoubleVector(SEXP args)
422
427
423
428
/* Then try as list */
424
429
ret = Tcl_ListObjGetElements (RTcl_interp , obj , & count , & elem );
425
- if (ret != TCL_OK ) /* didn't work, return NULL */
430
+ if (ret != TCL_OK || count > R_XLEN_T_MAX ) /* didn't work, return NULL */
426
431
return R_NilValue ;
427
-
428
- ans = allocVector (REALSXP , count );
432
+
433
+ ans = allocVector (REALSXP , ( R_xlen_t ) count );
429
434
for (i = 0 ; i < count ; i ++ ){
430
435
ret = Tcl_GetDoubleFromObj (RTcl_interp , elem [i ], & x );
431
436
if (ret != TCL_OK ) x = NA_REAL ;
@@ -470,9 +475,9 @@ SEXP RTcl_ObjFromDoubleVector(SEXP args)
470
475
471
476
SEXP RTcl_ObjAsIntVector (SEXP args )
472
477
{
473
- int count ;
478
+ Tcl_Size count , i ;
474
479
Tcl_Obj * * elem , * obj ;
475
- int ret , i ;
480
+ int ret ;
476
481
int x ;
477
482
SEXP ans ;
478
483
@@ -487,10 +492,10 @@ SEXP RTcl_ObjAsIntVector(SEXP args)
487
492
488
493
/* Then try as list */
489
494
ret = Tcl_ListObjGetElements (RTcl_interp , obj , & count , & elem );
490
- if (ret != TCL_OK ) /* didn't work, return NULL */
495
+ if (ret != TCL_OK || count > R_XLEN_T_MAX ) /* didn't work, return NULL */
491
496
return R_NilValue ;
492
-
493
- ans = allocVector (INTSXP , count );
497
+
498
+ ans = allocVector (INTSXP , ( R_xlen_t ) count );
494
499
for (i = 0 ; i < count ; i ++ ){
495
500
ret = Tcl_GetIntFromObj (RTcl_interp , elem [i ], & x );
496
501
if (ret != TCL_OK ) x = NA_INTEGER ;
@@ -525,7 +530,7 @@ SEXP RTcl_ObjFromIntVector(SEXP args)
525
530
526
531
SEXP RTcl_ObjAsRawVector (SEXP args )
527
532
{
528
- int nb , count , i , j ;
533
+ Tcl_Size count , nb , i , j ;
529
534
Tcl_Obj * * elem , * obj ;
530
535
unsigned char * ret ;
531
536
SEXP ans , el ;
@@ -536,18 +541,19 @@ SEXP RTcl_ObjAsRawVector(SEXP args)
536
541
if (!obj ) error (_ ("invalid tclObj -- perhaps saved from another session?" ));
537
542
ret = Tcl_GetByteArrayFromObj (obj , & nb );
538
543
if (ret ) {
539
- ans = allocVector (RAWSXP , nb );
544
+ ans = allocVector (RAWSXP , ( R_xlen_t ) nb );
540
545
for (j = 0 ; j < nb ; j ++ ) RAW (ans )[j ] = ret [j ];
541
546
return ans ;
542
547
}
543
548
544
549
/* Then try as list */
545
550
if (Tcl_ListObjGetElements (RTcl_interp , obj , & count , & elem )
546
551
!= TCL_OK ) return R_NilValue ;
547
-
548
- PROTECT (ans = allocVector (VECSXP , count ));
552
+ if (count > R_XLEN_T_MAX ) return R_NilValue ;
553
+
554
+ PROTECT (ans = allocVector (VECSXP , (R_xlen_t ) count ));
549
555
for (i = 0 ; i < count ; i ++ ) {
550
- el = allocVector (RAWSXP , nb );
556
+ el = allocVector (RAWSXP , ( R_xlen_t ) nb );
551
557
SET_VECTOR_ELT (ans , i , el );
552
558
ret = Tcl_GetByteArrayFromObj (elem [i ], & nb );
553
559
for (j = 0 ; j < nb ; j ++ ) RAW (el )[j ] = ret [j ];
0 commit comments