From e32e5326afb67ca4ab95d5b683e047b68a280105 Mon Sep 17 00:00:00 2001 From: Tom Rushworth Date: Sat, 7 Sep 2024 15:36:14 -0700 Subject: [PATCH 1/3] Add error messages to some silent failure cases in tests/test-str-source.c. --- tests/test-str-source.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test-str-source.c b/tests/test-str-source.c index adf965b..8a05e64 100644 --- a/tests/test-str-source.c +++ b/tests/test-str-source.c @@ -77,12 +77,15 @@ make_str_source(const char *str) str_handler_ctx *ctx; s = calloc(1, sizeof(*s)); - if (!s) + if (!s) { + fprintf(stderr,"Could calloc(1,sizeof(tre_str_source)\n"); return NULL; + } ctx = malloc(sizeof(str_handler_ctx)); if (!ctx) { + fprintf(stderr,"Could malloc(sizeof(str_handler_ctx)\n"); free(s); return NULL; } @@ -109,6 +112,7 @@ free_str_source(tre_str_source *s) static void test_reguexec(const char *str, const char *regex) { + reg_errcode_t ec; regex_t preg; tre_str_source *source; regmatch_t pmatch[5]; @@ -117,9 +121,15 @@ test_reguexec(const char *str, const char *regex) if (!source) return; - tre_regcomp(&preg, regex, REG_EXTENDED); - if (tre_reguexec(&preg, source, elementsof(pmatch), pmatch, 0) == 0) + ec = tre_regcomp(&preg, regex, REG_EXTENDED); + if (REG_OK != ec) { + fprintf(output_fd,"Pattern {%s} failed to compile, err code 0x%08x\n",regex,(unsigned)ec); + } + else if ((ec = tre_reguexec(&preg, source, elementsof(pmatch), pmatch, 0)) == 0) fprintf(output_fd,"Match: %d - %d\n", (int)pmatch[0].rm_so, (int)pmatch[0].rm_eo); + else { + fprintf(output_fd,"Match pattern {%s} against string {%s} failed, err code 0x%08x\n",regex,str,(unsigned)ec); + } free_str_source(source); tre_regfree(&preg); From 875ec62c68da5db21d3db53e2b923b931b22b011 Mon Sep 17 00:00:00 2001 From: Tom Rushworth Date: Tue, 10 Sep 2024 17:51:54 -0700 Subject: [PATCH 2/3] Add linkage sanity check code and (mostly) clean up indentation in tests/test-str-source.c --- tests/test-str-source.c | 79 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/tests/test-str-source.c b/tests/test-str-source.c index 8a05e64..064026b 100644 --- a/tests/test-str-source.c +++ b/tests/test-str-source.c @@ -78,14 +78,14 @@ make_str_source(const char *str) s = calloc(1, sizeof(*s)); if (!s) { - fprintf(stderr,"Could calloc(1,sizeof(tre_str_source)\n"); + fprintf(stderr,"Could not calloc(1,sizeof(tre_str_source)\n"); return NULL; } ctx = malloc(sizeof(str_handler_ctx)); if (!ctx) { - fprintf(stderr,"Could malloc(sizeof(str_handler_ctx)\n"); + fprintf(stderr,"Could not malloc(sizeof(str_handler_ctx)\n"); free(s); return NULL; } @@ -122,13 +122,13 @@ test_reguexec(const char *str, const char *regex) return; ec = tre_regcomp(&preg, regex, REG_EXTENDED); - if (REG_OK != ec) { - fprintf(output_fd,"Pattern {%s} failed to compile, err code 0x%08x\n",regex,(unsigned)ec); + if (ec != REG_OK) { + fprintf(output_fd,"Pattern {%s} failed to compile, err code 0x%08x\n", regex, (unsigned)ec); } else if ((ec = tre_reguexec(&preg, source, elementsof(pmatch), pmatch, 0)) == 0) fprintf(output_fd,"Match: %d - %d\n", (int)pmatch[0].rm_so, (int)pmatch[0].rm_eo); else { - fprintf(output_fd,"Match pattern {%s} against string {%s} failed, err code 0x%08x\n",regex,str,(unsigned)ec); + fprintf(output_fd,"Match pattern {%s} against string {%s} failed, err code 0x%08x\n", regex, str, (unsigned)ec); } free_str_source(source); @@ -139,6 +139,12 @@ int main(int argc, char **argv) { int ch; + int rc; + int query; + int int_res; + int desired_config; + int config_mismatch = 0; + char *char_res; output_fd = stdout; while (EOF != (ch = getopt(argc,argv,"o:"))) { switch (ch) { @@ -157,10 +163,73 @@ main(int argc, char **argv) exit(1); } } + /* Display the version and configuation of the TRE library we are using. */ + fprintf(output_fd,"Using TRE library version: %s\n",tre_version()); + fprintf(output_fd," test compiled for version: %s\n",TRE_VERSION); + + query = (int) TRE_CONFIG_APPROX; + int_res = -1; + rc = tre_config(query,&int_res); +#ifdef TRE_APPROX + desired_config = 1; +#else + desired_config = 0; +#endif + fprintf(output_fd," TRE_CONFIG_APPROX: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + config_mismatch |= (int_res != desired_config); + + query = (int) TRE_CONFIG_WCHAR; + int_res = -1; + rc = tre_config(query,&int_res); +#ifdef TRE_WCHAR + desired_config = 1; +#else + desired_config = 0; +#endif + fprintf(output_fd," TRE_CONFIG_WCHAR: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + config_mismatch |= (int_res != desired_config); + + query = (int) TRE_CONFIG_MULTIBYTE; + int_res = -1; + rc = tre_config(query,&int_res); +#ifdef TRE_MULTIBYTE + desired_config = 1; +#else + desired_config = 0; +#endif + fprintf(output_fd," TRE_CONFIG_MULTIBYTE: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + config_mismatch |= (int_res != desired_config); + + query = (int) TRE_CONFIG_SYSTEM_ABI; + int_res = -1; + rc = tre_config(query,&int_res); +#ifdef TRE_CONFIG_SYSTEM_ABI + desired_config = 1; +#else + desired_config = 0; +#endif + fprintf(output_fd," TRE_CONFIG_SYSTEM_ABI: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + config_mismatch |= (int_res != desired_config); + + query = (int) TRE_CONFIG_VERSION; + char_res = "---"; + rc = tre_config(query,&char_res); + fprintf(output_fd," TRE_CONFIG_VERSION: {%s} (rc=%d)\n",char_res,rc); + + if (config_mismatch) { + fprintf(output_fd,"WARNING: The configuration this test program was compiled with is not the same"); + fprintf(output_fd,"as the configuration of the libtre being used, some tests may be incorrect.\n"); + } + + fprintf(output_fd,"------------------ Test 1: should Match: 6 - 12\n"); test_reguexec("xfoofofoofoo","(foo)\\1"); + fprintf(output_fd,"------------------ Test 2: should Match: 0 - 6\n"); test_reguexec("catcat","(cat|dog)\\1"); + fprintf(output_fd,"------------------ Test 3: should not match\n"); test_reguexec("catdog","(cat|dog)\\1"); + fprintf(output_fd,"------------------ Test 4: should Match: 0 - 6\n"); test_reguexec("dogdog","(cat|dog)\\1"); + fprintf(output_fd,"------------------ Test 5: should not match\n"); test_reguexec("dogcat","(cat|dog)\\1"); if (NULL != output_fd && stdout != output_fd) { From 1c23e69ab303534c9bc4d1a6af9f30f8bfd4a4bd Mon Sep 17 00:00:00 2001 From: Tom Rushworth Date: Tue, 10 Sep 2024 18:15:45 -0700 Subject: [PATCH 3/3] Commit correct version of tests/test-str-source.c --- tests/test-str-source.c | 95 +++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/tests/test-str-source.c b/tests/test-str-source.c index 064026b..f507e8d 100644 --- a/tests/test-str-source.c +++ b/tests/test-str-source.c @@ -1,5 +1,8 @@ /* test-str-source.c - Sample program for using tre_reguexec() + It also serves as a sanity check for correct + linkage when multiple variants of the TRE library + are present. This software is released under a BSD-style license. See the file LICENSE for details and copyright. @@ -77,10 +80,11 @@ make_str_source(const char *str) str_handler_ctx *ctx; s = calloc(1, sizeof(*s)); - if (!s) { - fprintf(stderr,"Could not calloc(1,sizeof(tre_str_source)\n"); - return NULL; - } + if (!s) + { + fprintf(stderr,"Could not calloc(1,sizeof(tre_str_source)\n"); + return NULL; + } ctx = malloc(sizeof(str_handler_ctx)); if (!ctx) @@ -122,14 +126,16 @@ test_reguexec(const char *str, const char *regex) return; ec = tre_regcomp(&preg, regex, REG_EXTENDED); - if (ec != REG_OK) { - fprintf(output_fd,"Pattern {%s} failed to compile, err code 0x%08x\n", regex, (unsigned)ec); - } + if (ec != REG_OK) + { + fprintf(output_fd,"Pattern {%s} failed to compile, err code 0x%08x\n", regex, (unsigned)ec); + } else if ((ec = tre_reguexec(&preg, source, elementsof(pmatch), pmatch, 0)) == 0) fprintf(output_fd,"Match: %d - %d\n", (int)pmatch[0].rm_so, (int)pmatch[0].rm_eo); - else { - fprintf(output_fd,"Match pattern {%s} against string {%s} failed, err code 0x%08x\n", regex, str, (unsigned)ec); - } + else + { + fprintf(output_fd,"Match pattern {%s} against string {%s} failed, err code 0x%08x\n", regex, str, (unsigned)ec); + } free_str_source(source); tre_regfree(&preg); @@ -146,26 +152,31 @@ main(int argc, char **argv) int config_mismatch = 0; char *char_res; output_fd = stdout; - while (EOF != (ch = getopt(argc,argv,"o:"))) { - switch (ch) { - case 'o': - if (NULL == (output_fd = fopen(optarg,"w"))) { - fprintf(stderr,"Could not open {%s} for output, quitting\n",optarg); - exit(1); - } - break; - default: - fprintf(stderr,"Invalid command line option '-%c', quitting\n",ch); - if (NULL != output_fd && stdout != output_fd) { - fclose(output_fd); - output_fd = NULL; - } - exit(1); + while ((ch = getopt(argc,argv,"o:") != EOF)) + { + switch (ch) + { + case 'o': + if ((output_fd = fopen(optarg,"w")) == NULL) + { + fprintf(stderr,"Could not open {%s} for output, quitting\n", optarg); + exit(1); + } + break; + default: + fprintf(stderr,"Invalid command line option '-%c', quitting\n", ch); + if (output_fd != NULL && stdout != output_fd) + { + fclose(output_fd); + output_fd = NULL; + } + exit(1); + } } - } + /* Display the version and configuation of the TRE library we are using. */ - fprintf(output_fd,"Using TRE library version: %s\n",tre_version()); - fprintf(output_fd," test compiled for version: %s\n",TRE_VERSION); + fprintf(output_fd,"Using TRE library version: %s\n", tre_version()); + fprintf(output_fd," test compiled for version: %s\n", TRE_VERSION); query = (int) TRE_CONFIG_APPROX; int_res = -1; @@ -175,7 +186,7 @@ main(int argc, char **argv) #else desired_config = 0; #endif - fprintf(output_fd," TRE_CONFIG_APPROX: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + fprintf(output_fd," TRE_CONFIG_APPROX: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config); config_mismatch |= (int_res != desired_config); query = (int) TRE_CONFIG_WCHAR; @@ -186,7 +197,7 @@ main(int argc, char **argv) #else desired_config = 0; #endif - fprintf(output_fd," TRE_CONFIG_WCHAR: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + fprintf(output_fd," TRE_CONFIG_WCHAR: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config); config_mismatch |= (int_res != desired_config); query = (int) TRE_CONFIG_MULTIBYTE; @@ -197,7 +208,7 @@ main(int argc, char **argv) #else desired_config = 0; #endif - fprintf(output_fd," TRE_CONFIG_MULTIBYTE: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + fprintf(output_fd," TRE_CONFIG_MULTIBYTE: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config); config_mismatch |= (int_res != desired_config); query = (int) TRE_CONFIG_SYSTEM_ABI; @@ -208,18 +219,19 @@ main(int argc, char **argv) #else desired_config = 0; #endif - fprintf(output_fd," TRE_CONFIG_SYSTEM_ABI: library %d (rc=%d) test compiled for %d\n",int_res,rc,desired_config); + fprintf(output_fd," TRE_CONFIG_SYSTEM_ABI: library %d (rc=%d) test compiled for %d\n", int_res, rc, desired_config); config_mismatch |= (int_res != desired_config); query = (int) TRE_CONFIG_VERSION; char_res = "---"; rc = tre_config(query,&char_res); - fprintf(output_fd," TRE_CONFIG_VERSION: {%s} (rc=%d)\n",char_res,rc); + fprintf(output_fd," TRE_CONFIG_VERSION: {%s} (rc=%d)\n", char_res, rc); - if (config_mismatch) { - fprintf(output_fd,"WARNING: The configuration this test program was compiled with is not the same"); - fprintf(output_fd,"as the configuration of the libtre being used, some tests may be incorrect.\n"); - } + if (config_mismatch) + { + fprintf(output_fd,"WARNING: The configuration this test program was compiled with is not the same"); + fprintf(output_fd,"as the configuration of the libtre being used, some tests may be incorrect.\n"); + } fprintf(output_fd,"------------------ Test 1: should Match: 6 - 12\n"); test_reguexec("xfoofofoofoo","(foo)\\1"); @@ -232,9 +244,10 @@ main(int argc, char **argv) fprintf(output_fd,"------------------ Test 5: should not match\n"); test_reguexec("dogcat","(cat|dog)\\1"); - if (NULL != output_fd && stdout != output_fd) { - fclose(output_fd); - output_fd = NULL; - } + if (NULL != output_fd && stdout != output_fd) + { + fclose(output_fd); + output_fd = NULL; + } return 0; }