@@ -724,7 +724,6 @@ ly_pat_compile_posix(const char *pattern, void **pat_comp, struct ly_err_item **
724724static LY_ERR
725725ly_pat_compile_xmlschema_chblocks_xmlschema2perl (const char * pattern , char * * regex , struct ly_err_item * * err )
726726{
727- #define URANGE_LEN 19
728727 char * ublock2urange [][2 ] = {
729728 {"BasicLatin" , "[\\x{0000}-\\x{007F}]" },
730729 {"Latin-1Supplement" , "[\\x{0080}-\\x{00FF}]" },
@@ -829,15 +828,6 @@ ly_pat_compile_xmlschema_chblocks_xmlschema2perl(const char *pattern, char **reg
829828 }
830829 end = (ptr - perl_regex ) + 1 ;
831830
832- /* need more space */
833- if (end - start < URANGE_LEN ) {
834- perl_regex = ly_realloc (perl_regex , strlen (perl_regex ) + (URANGE_LEN - (end - start )) + 1 );
835- * regex = perl_regex ;
836- if (!perl_regex ) {
837- return ly_err_new (err , LY_EMEM , 0 , NULL , NULL , LY_EMEM_MSG );
838- }
839- }
840-
841831 /* find our range */
842832 for (idx = 0 ; ublock2urange [idx ][0 ]; ++ idx ) {
843833 if (!strncmp (perl_regex + start + ly_strlen_const ("\\p{Is" ),
@@ -851,6 +841,17 @@ ly_pat_compile_xmlschema_chblocks_xmlschema2perl(const char *pattern, char **reg
851841 }
852842 ublock = idx ;
853843
844+ /* need more space */
845+ size_t urange_len = strlen (ublock2urange [ublock ][1 ]);
846+
847+ if (end - start < urange_len ) {
848+ perl_regex = ly_realloc (perl_regex , strlen (perl_regex ) + (urange_len - (end - start )) + 1 );
849+ * regex = perl_regex ;
850+ if (!perl_regex ) {
851+ return ly_err_new (err , LY_EMEM , 0 , NULL , NULL , LY_EMEM_MSG );
852+ }
853+ }
854+
854855 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
855856 for (idx2 = 0 , idx = 0 ; idx2 < start ; ++ idx2 ) {
856857 if ((perl_regex [idx2 ] == '[' ) && (!idx2 || (perl_regex [idx2 - 1 ] != '\\' ))) {
@@ -863,11 +864,11 @@ ly_pat_compile_xmlschema_chblocks_xmlschema2perl(const char *pattern, char **reg
863864 }
864865 if (idx ) {
865866 /* skip brackets */
866- memmove (perl_regex + start + (URANGE_LEN - 2 ), perl_regex + end , strlen (perl_regex + end ) + 1 );
867- memcpy (perl_regex + start , ublock2urange [ublock ][1 ] + 1 , URANGE_LEN - 2 );
867+ memmove (perl_regex + start + (urange_len - 2 ), perl_regex + end , strlen (perl_regex + end ) + 1 );
868+ memcpy (perl_regex + start , ublock2urange [ublock ][1 ] + 1 , urange_len - 2 );
868869 } else {
869- memmove (perl_regex + start + URANGE_LEN , perl_regex + end , strlen (perl_regex + end ) + 1 );
870- memcpy (perl_regex + start , ublock2urange [ublock ][1 ], URANGE_LEN );
870+ memmove (perl_regex + start + urange_len , perl_regex + end , strlen (perl_regex + end ) + 1 );
871+ memcpy (perl_regex + start , ublock2urange [ublock ][1 ], urange_len );
871872 }
872873 }
873874
0 commit comments