@@ -454,45 +454,71 @@ void PerlOMP_VERIFY_2D_AoA(SV *AoA) {
454454 }
455455}
456456
457- /* Helper function to verify element types */
458- bool is_float (SV * sv ) { return SvNOK (sv ); }
459- bool is_int (SV * sv ) { return SvIOK (sv ); }
460- bool is_string (SV * sv ) { return SvPOK (sv ); }
457+ /* Checks if an SV is an integer, allowing for stored floats that are whole numbers */
458+ bool is_int (SV * sv ) {
459+ return SvIOK (sv ) || (SvNOK (sv ) && SvIV (sv ) == SvNV (sv ));
460+ }
461+
462+ /* Checks if an SV is a float, including cases where Perl stores it as an integer */
463+ bool is_float (SV * sv ) {
464+ return SvNOK (sv ) || (SvIOK (sv ) && SvIV (sv ) != SvNV (sv ));
465+ }
466+
467+ /* Checks if an SV is a string, ensuring it's not just a numeric representation */
468+ bool is_string (SV * sv ) {
469+ return SvPOK (sv ) || (!SvNOK (sv ) && !SvIOK (sv ));
470+ }
461471
462- /* Generic function to verify a 1D array's element type */
463472void verify_1D_array_type (SV * array , bool (* type_check )(SV * ), const char * type_name ) {
464473 if (!is_array_ref (array )) {
465474 croak ("Expected a 1D array reference" );
466475 }
467476 AV * av = (AV * )SvRV (array );
468- I32 len = av_len (av ) + 1 ;
477+ I32 len = av_len (av );
478+ if (len == -1 ) return ; // Handle empty arrays safely
479+ len ++ ; // Convert last index to total count
480+
469481 for (I32 i = 0 ; i < len ; i ++ ) {
470482 SV * * element = av_fetch (av , i , 0 );
471- if (!element || !type_check (* element )) {
472- croak ("Expected all elements to be %s at index %d" , type_name , i );
483+ if (!element || !* element || ! type_check (* element )) {
484+ croak ("Expected all elements to be %s at index %" IVdf , type_name , ( IV ) i );
473485 }
474486 }
475487}
476488
477489/* Implement type-specific 1D array verifications */
478490void PerlOMP_VERIFY_1D_FLOAT_ARRAY (SV * array ) { verify_1D_array_type (array , is_float , "float" ); }
479491void PerlOMP_VERIFY_1D_INT_ARRAY (SV * array ) { verify_1D_array_type (array , is_int , "integer" ); }
480- void PerlOMP_VERIFY_1D_DOUBLE_ARRAY (SV * array ) { verify_1D_array_type (array , is_float , "double" ); }
481492void PerlOMP_VERIFY_1D_STRING_ARRAY (SV * array ) { verify_1D_array_type (array , is_string , "string" ); }
482493
483494/* Generic function to verify a 2D array's element type */
484495void verify_2D_array_type (SV * AoA , bool (* type_check )(SV * ), const char * type_name ) {
485- PerlOMP_VERIFY_2D_AoA (AoA );
496+ PerlOMP_VERIFY_2D_AoA (AoA ); // Assuming this macro validates AoA correctly
497+
498+ if (!is_array_ref (AoA )) {
499+ croak ("Expected a 2D array reference" );
500+ }
501+
486502 AV * outer = (AV * )SvRV (AoA );
487- I32 rows = av_len (outer ) + 1 ;
503+ I32 rows = av_len (outer );
504+ if (rows == -1 ) return ; // Handle empty outer array safely
505+ rows ++ ; // Convert last index to total count
506+
488507 for (I32 i = 0 ; i < rows ; i ++ ) {
489508 SV * * inner_ref = av_fetch (outer , i , 0 );
509+ if (!inner_ref || !* inner_ref || !is_array_ref (* inner_ref )) {
510+ croak ("Expected an array reference at row %" IVdf , (IV )i );
511+ }
512+
490513 AV * inner = (AV * )SvRV (* inner_ref );
491- I32 cols = av_len (inner ) + 1 ;
514+ I32 cols = av_len (inner );
515+ if (cols == -1 ) continue ; // Handle empty inner arrays safely
516+ cols ++ ; // Convert last index to total count
517+
492518 for (I32 j = 0 ; j < cols ; j ++ ) {
493519 SV * * element = av_fetch (inner , j , 0 );
494- if (!element || !type_check (* element )) {
495- croak ("Expected all elements to be %s at [%d ][%d ]" , type_name , i , j );
520+ if (!element || !* element || ! type_check (* element )) {
521+ croak ("Expected all elements to be %s at [%" IVdf " ][%" IVdf " ]" , type_name , ( IV ) i , ( IV ) j );
496522 }
497523 }
498524 }
@@ -501,6 +527,5 @@ void verify_2D_array_type(SV *AoA, bool (*type_check)(SV *), const char *type_na
501527/* Implement type-specific 2D array verifications */
502528void PerlOMP_VERIFY_2D_FLOAT_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_float , "float" ); }
503529void PerlOMP_VERIFY_2D_INT_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_int , "integer" ); }
504- void PerlOMP_VERIFY_2D_DOUBLE_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_float , "double" ); }
505530void PerlOMP_VERIFY_2D_STRING_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_string , "string" ); }
506531
0 commit comments