Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove multiple calls to free when successively calling jq_reset. #3134

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ static void jq_reset(jq_state *jq) {

jq->halted = 0;
jv_free(jq->exit_code);
jq->exit_code = jv_invalid();
jv_free(jq->error_message);
jq->error_message = jv_invalid();
if (jv_get_kind(jq->path) != JV_KIND_INVALID)
jv_free(jq->path);
jq->path = jv_null();
Expand Down
62 changes: 62 additions & 0 deletions src/jq_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

static void jv_test();
static void run_jq_tests(jv, int, FILE *, int, int);
static void run_jq_start_state_tests();
#ifdef HAVE_PTHREAD
static void run_jq_pthread_tests();
#endif
Expand Down Expand Up @@ -37,6 +38,7 @@ int jq_testsuite(jv libdirs, int verbose, int argc, char* argv[]) {
}
}
run_jq_tests(libdirs, verbose, testdata, skip, take);
run_jq_start_state_tests();
#ifdef HAVE_PTHREAD
run_jq_pthread_tests();
#endif
Expand Down Expand Up @@ -251,6 +253,66 @@ static void run_jq_tests(jv lib_dirs, int verbose, FILE *testdata, int skip, int
}


static int test_start_state(jq_state *jq, char *prog) {
int pass = 1;
jv message = jq_get_error_message(jq);
if (jv_is_valid(message)) {
printf("*** Expected error_message to be invalid after jq_start: %s\n", prog);
pass = 0;
}
jv_free(message);

jv exit_code = jq_get_exit_code(jq);
if (jv_is_valid(exit_code)) {
printf("*** Expected exit_code to be invalid after jq_start: %s\n", prog);
pass = 0;
}
jv_free(exit_code);

if (jq_halted(jq)) {
printf("*** Expected jq to not be halted after jq_start: %s\n", prog);
pass = 0;
}

return pass;
}

// Test jq_state is reset after subsequent calls to jq_start.
static void test_jq_start_resets_state(char *prog, const char *input) {
printf("Test jq_state: %s\n", prog);
jq_state *jq = jq_init();
assert(jq);

int compiled = jq_compile(jq, prog);
assert(compiled);

// First call to jq_start. Run until completion.
jv parsed_input = jv_parse(input);
assert(jv_is_valid(parsed_input));
jq_start(jq, parsed_input, 0);
assert(test_start_state(jq, prog));
while (1) {
jv result = jq_next(jq);
int valid = jv_is_valid(result);
jv_free(result);
if (!valid) { break; }
}

// Second call to jq_start.
jv parsed_input2 = jv_parse(input);
assert(jv_is_valid(parsed_input2));
jq_start(jq, parsed_input2, 0);
assert(test_start_state(jq, prog));

jq_teardown(&jq);
}

static void run_jq_start_state_tests() {
test_jq_start_resets_state(".[]", "[1,2,3]");
test_jq_start_resets_state(".[] | if .%2 == 0 then halt_error else . end", "[1,2,3]");
}


/// pthread regression test
#ifdef HAVE_PTHREAD
#define NUMBER_OF_THREADS 3
Expand Down
Loading