Skip to content
Myr

Fix crashes and resource leaks in `jq_testsuite` (#3509)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Fix segfault when --skip/--take is the last argument. - Fix FILE handle leak: opened test files were never closed. - Support running multiple test files in one invocation.

authored by itchyny · committed by GitHub · parent b6a9e26 · Browse files · 1 file+25 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+25 added−16 removed

  • other:+25 added−16 removed

1 file+25 added−16 removed

modified:src/jq_test.c+25 added-16 removed
17 unmodified lines
18static void run_jq_pthread_tests(void);19#endif2021int jq_testsuite(jv libdirs, int verbose, int argc, char* argv[]) {22 FILE *testdata = stdin;22 int skip = -1;23 int take = -1;24 int nfiles = 0;25 jv_test();26 if (argc > 0) {27 for(int i = 0; i < argc; i++) {28 if (!strcmp(argv[i], "--skip")) {29 skip = atoi(argv[i+1]);30 i++;31 } else if (!strcmp(argv[i], "--take")) {32 take = atoi(argv[i+1]);33 i++;34 } else {35 testdata = fopen(argv[i], "r");36 if (!testdata) {37 perror("fopen");38 exit(1);39 }26 for (int i = 0; i < argc; i++) {27 if (!strcmp(argv[i], "--skip")) {28 if (++i >= argc) {29 fprintf(stderr, "--skip requires an argument\n");30 exit(1);31 }32 skip = atoi(argv[i]);33 } else if (!strcmp(argv[i], "--take")) {34 if (++i >= argc) {35 fprintf(stderr, "--take requires an argument\n");36 exit(1);37 }38 take = atoi(argv[i]);39 } else {40 FILE *testdata = fopen(argv[i], "r");41 if (!testdata) {42 perror("fopen");43 exit(1);44 }45 run_jq_tests(jv_copy(libdirs), verbose, testdata, skip, take);46 fclose(testdata);47 nfiles++;48 }49 }43 run_jq_tests(libdirs, verbose, testdata, skip, take);50 if (nfiles == 0)51 run_jq_tests(jv_copy(libdirs), verbose, stdin, skip, take);52 jv_free(libdirs);53 run_jq_start_state_tests();54 run_jq_compile_args_tests();55 run_jq_recompile_tests();56 run_jq_exhaust_and_reuse_tests();
707 unmodified lines