Skip to content

Commit 931b7b8

Browse files
authored
common BUGFIX Unicode Specials block pattern support (CESNET#2510)
* Correction of unicode block range bug * Unicode Specials block bug correction * Use original char array with strlen to map unicode block to unicode range
1 parent b6ea2aa commit 931b7b8

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/ly_common.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,6 @@ ly_pat_compile_posix(const char *pattern, void **pat_comp, struct ly_err_item **
724724
static LY_ERR
725725
ly_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

tests/utests/types/string.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,22 @@ test_data_xml(void **state)
863863
UTEST_INVALID_MODULE(schema, LYS_IN_YANG, NULL, LY_EVALID);
864864
CHECK_LOG_CTX("Regular expression \"[\\p{IsBasicLatin}\\p{IsUnknownUnicodeBlock}]+\" "
865865
"is not valid (\"UnknownUnicodeBlock}]+\": unknown block name).", "/T_UB_8:port", 0);
866+
867+
schema = MODULE_CREATE_YANG("T_UB_9", "leaf port {type string { pattern "
868+
"'[\\p{IsSpecials}]+';}}");
869+
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
870+
TEST_SUCCESS_XML("T_UB_9", "&#xFFFA;&#xFFFD;", STRING, "\xef\xbf\xba\xef\xbf\xbd");
871+
872+
schema = MODULE_CREATE_YANG("T_UB_10", "leaf port {type string { pattern "
873+
"'[\\p{IsSpecials}]+';}}");
874+
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
875+
TEST_ERROR_XML("T_UB_10", "&#xFFDF;&#xFFFD;");
876+
CHECK_LOG_CTX("Unsatisfied pattern - \"\xef\xbf\x9f\xef\xbf\xbd\" does not match \"[\\p{IsSpecials}]+\".", "/T_UB_10:port", 1);
877+
878+
schema = MODULE_CREATE_YANG("T_UB_11", "leaf port {type string { pattern "
879+
"'[\\p{IsSpecials}]+';}}");
880+
UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, NULL);
881+
TEST_SUCCESS_XML("T_UB_11", "&#xFEFF;&#xFFFD;", STRING, "\xef\xbb\xbf\xef\xbf\xbd");
866882
}
867883

868884
static void

0 commit comments

Comments
 (0)