X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=tools%2Fccanlint%2Ftests%2Ftests_pass_valgrind.c;h=2f4df18b47e4382a3ea8d7032f78411fece3b536;hb=6aa2f4e347e5d66a392b879fe901bc582099a552;hp=bf914856845f2a7a42e9cec97d210c9b7c3ef06a;hpb=13911ef769824a0958e1f1ce7292dfc8f23feb18;p=ccan diff --git a/tools/ccanlint/tests/tests_pass_valgrind.c b/tools/ccanlint/tests/tests_pass_valgrind.c index bf914856..2f4df18b 100644 --- a/tools/ccanlint/tests/tests_pass_valgrind.c +++ b/tools/ccanlint/tests/tests_pass_valgrind.c @@ -1,10 +1,9 @@ #include #include -#include #include +#include #include -#include -#include +#include #include "tests_pass.h" #include #include @@ -18,17 +17,28 @@ #include #include -REGISTER_TEST(tests_pass_valgrind); -REGISTER_TEST(tests_pass_valgrind_noleaks); - /* Note: we already test safe_mode in run_tests.c */ static const char *can_run_vg(struct manifest *m) { if (!do_valgrind) - return talloc_asprintf(m, "No valgrind support"); + return tal_fmt(m, "No valgrind support"); return NULL; } +static void do_leakcheck_vg(struct manifest *m, + unsigned int *timeleft, + struct score *score); + +static struct ccanlint tests_pass_valgrind_noleaks = { + .key = "tests_pass_valgrind_noleaks", + .name = "Module's run and api tests have no memory leaks", + .check = do_leakcheck_vg, + .takes_options = true, + .needs = "tests_pass_valgrind" +}; +REGISTER_TEST(tests_pass_valgrind_noleaks); + + /* Example output: ==2749== Conditional jump or move depends on uninitialised value(s) ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263) @@ -51,35 +61,34 @@ static bool blank_line(const char *line) static char **extract_matching(const char *prefix, char *lines[]) { unsigned int i, num_ret = 0; - char **ret = talloc_array(lines, char *, talloc_array_length(lines)); + char **ret = tal_arr(lines, char *, tal_count(lines)); - for (i = 0; i < talloc_array_length(lines) - 1; i++) { + for (i = 0; i < tal_count(lines) - 1; i++) { if (strstarts(lines[i], prefix)) { - ret[num_ret++] = talloc_steal(ret, lines[i]); + ret[num_ret++] = tal_strdup(ret, lines[i]); lines[i] = (char *)""; } } ret[num_ret++] = NULL; - /* Make sure length is correct! */ - return talloc_realloc(NULL, ret, char *, num_ret); + /* Make sure tal_count is correct! */ + tal_resize(&ret, num_ret); + return ret; } static char *get_leaks(char *lines[], char **errs) { - char *leaks = talloc_strdup(lines, ""); + char *leaks = tal_strdup(lines, ""); unsigned int i; - for (i = 0; i < talloc_array_length(lines) - 1; i++) { + for (i = 0; i < tal_count(lines) - 1; i++) { if (strstr(lines[i], " lost ")) { /* A leak... */ if (strstr(lines[i], " definitely lost ")) { /* Definite leak, report. */ while (lines[i] && !blank_line(lines[i])) { - leaks = talloc_append_string(leaks, - lines[i]); - leaks = talloc_append_string(leaks, - "\n"); + tal_append_fmt(&leaks, "%s\n", + lines[i]); i++; } } else @@ -89,8 +98,10 @@ static char *get_leaks(char *lines[], char **errs) } else { /* A real error. */ while (lines[i] && !blank_line(lines[i])) { - *errs = talloc_append_string(*errs, lines[i]); - *errs = talloc_append_string(*errs, "\n"); + if (!*errs) + *errs = tal_fmt(NULL, "%s\n", lines[i]); + else + tal_append_fmt(errs, "%s\n", lines[i]); i++; } } @@ -101,12 +112,12 @@ static char *get_leaks(char *lines[], char **errs) /* Returns leaks, and sets errs[] */ static char *analyze_output(const char *output, char **errs) { - char *leaks = talloc_strdup(output, ""); + char *leaks = tal_strdup(output, ""); unsigned int i; - char **lines = strsplit(output, output, "\n"); + char **lines = tal_strsplit(output, output, "\n", STR_EMPTY_OK); - *errs = talloc_strdup(output, ""); - for (i = 0; i < talloc_array_length(lines) - 1; i++) { + *errs = tal_strdup(output, ""); + for (i = 0; i < tal_count(lines) - 1; i++) { unsigned int preflen = strspn(lines[i], "=0123456789"); char *prefix, **sublines; @@ -114,18 +125,19 @@ static char *analyze_output(const char *output, char **errs) if (preflen == 0) continue; - prefix = talloc_strndup(output, lines[i], preflen); + prefix = tal_strndup(output, lines[i], preflen); sublines = extract_matching(prefix, lines); - leaks = talloc_append_string(leaks, get_leaks(sublines, errs)); + leaks = tal_strcat(output, take(leaks), + take(get_leaks(sublines, errs))); } if (!leaks[0]) { - talloc_free(leaks); + tal_free(leaks); leaks = NULL; } if (!(*errs)[0]) { - talloc_free(*errs); + tal_free(*errs); *errs = NULL; } return leaks; @@ -134,19 +146,18 @@ static char *analyze_output(const char *output, char **errs) static const char *concat(struct score *score, char *bits[]) { unsigned int i; - char *ret = talloc_strdup(score, ""); + char *ret = tal_strdup(score, ""); for (i = 0; bits[i]; i++) { if (i) - ret = talloc_append_string(ret, " "); - ret = talloc_append_string(ret, bits[i]); + ret = tal_strcat(score, take(ret), " "); + ret = tal_strcat(score, take(ret), bits[i]); } return ret; } /* FIXME: Run examples, too! */ static void do_run_tests_vg(struct manifest *m, - bool keep, unsigned int *timeleft, struct score *score) { @@ -170,10 +181,7 @@ static void do_run_tests_vg(struct manifest *m, continue; } - if (keep) - talloc_set_destructor(i->valgrind_log, NULL); - - output = grab_file(i, i->valgrind_log, NULL); + output = grab_file(i, i->valgrind_log); /* No valgrind errors? */ if (!output || output[0] == '\0') { err = NULL; @@ -191,7 +199,6 @@ static void do_run_tests_vg(struct manifest *m, } static void do_leakcheck_vg(struct manifest *m, - bool keep, unsigned int *timeleft, struct score *score) { @@ -244,11 +251,10 @@ static void run_under_debugger_vg(struct manifest *m, struct score *score) return; first = list_top(&score->per_file_errors, struct file_error, list); - command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s", - concat(score, - per_file_options(&tests_pass_valgrind, - first->file)), - first->file->compiled[COMPILE_NORMAL]); + command = tal_fmt(m, "valgrind --leak-check=full --db-attach=yes%s %s %s", + concat(score, per_file_options(&tests_pass_valgrind, + first->file)), + valgrind_suppress, first->file->compiled[COMPILE_NORMAL]); if (system(command)) doesnt_matter(); } @@ -262,12 +268,4 @@ struct ccanlint tests_pass_valgrind = { .takes_options = true, .needs = "tests_pass" }; - -struct ccanlint tests_pass_valgrind_noleaks = { - .key = "tests_pass_valgrind_noleaks", - .name = "Module's run and api tests have no memory leaks", - .check = do_leakcheck_vg, - .takes_options = true, - .needs = "tests_pass_valgrind" -}; - +REGISTER_TEST(tests_pass_valgrind);