Commit 6fa62e5a authored by Rusty Russell's avatar Rusty Russell

opt: fix memory leak in tests.

No only does this give us one more ccanlint point, it clears the way
to see if we introduce a *real* memory leak later.
parent e34192d5
...@@ -42,6 +42,8 @@ int main(int argc, char *argv[]) ...@@ -42,6 +42,8 @@ int main(int argc, char *argv[])
free(err_output); free(err_output);
err_output = NULL; err_output = NULL;
/* parse_args allocates argv */
free(argv);
return exit_status(); return exit_status();
} }
...@@ -19,6 +19,9 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...); ...@@ -19,6 +19,9 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...);
#define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap) #define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap)
static int saved_vprintf(const char *fmt, va_list ap); static int saved_vprintf(const char *fmt, va_list ap);
#define malloc(size) saved_malloc(size)
static void *saved_malloc(size_t size);
#include <ccan/opt/helpers.c> #include <ccan/opt/helpers.c>
#include <ccan/opt/opt.c> #include <ccan/opt/opt.c>
#include <ccan/opt/usage.c> #include <ccan/opt/usage.c>
...@@ -69,6 +72,13 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...) ...@@ -69,6 +72,13 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...)
return ret; return ret;
} }
#undef malloc
static void *last_allocation;
static void *saved_malloc(size_t size)
{
return last_allocation = malloc(size);
}
/* Test helpers. */ /* Test helpers. */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
...@@ -220,15 +230,25 @@ int main(int argc, char *argv[]) ...@@ -220,15 +230,25 @@ int main(int argc, char *argv[])
reset_options(); reset_options();
opt_register_noarg("-a", opt_register_noarg("-a",
opt_version_and_exit, "1.2.3", ""); opt_version_and_exit, "1.2.3", "");
/* parse_args allocates argv */
free(argv);
argc = 2;
argv = malloc(sizeof(argv[0]) * 3);
argv[0] = "thisprog";
argv[1] = "-a";
argv[2] = NULL;
exitval = setjmp(exited); exitval = setjmp(exited);
if (exitval == 0) { if (exitval == 0) {
parse_args(&argc, &argv, "-a", NULL); opt_parse(&argc, argv, save_err_output);
fail("opt_show_version_and_exit returned?"); fail("opt_show_version_and_exit returned?");
} else { } else {
ok1(exitval - 1 == 0); ok1(exitval - 1 == 0);
} }
ok1(strcmp(output, "1.2.3\n") == 0); ok1(strcmp(output, "1.2.3\n") == 0);
free(output); free(output);
free(argv);
output = NULL; output = NULL;
} }
...@@ -238,9 +258,16 @@ int main(int argc, char *argv[]) ...@@ -238,9 +258,16 @@ int main(int argc, char *argv[])
reset_options(); reset_options();
opt_register_noarg("-a", opt_register_noarg("-a",
opt_usage_and_exit, "[args]", ""); opt_usage_and_exit, "[args]", "");
argc = 2;
argv = malloc(sizeof(argv[0]) * 3);
argv[0] = "thisprog";
argv[1] = "-a";
argv[2] = NULL;
exitval = setjmp(exited); exitval = setjmp(exited);
if (exitval == 0) { if (exitval == 0) {
parse_args(&argc, &argv, "-a", NULL); opt_parse(&argc, argv, save_err_output);
fail("opt_usage_and_exit returned?"); fail("opt_usage_and_exit returned?");
} else { } else {
ok1(exitval - 1 == 0); ok1(exitval - 1 == 0);
...@@ -249,6 +276,9 @@ int main(int argc, char *argv[]) ...@@ -249,6 +276,9 @@ int main(int argc, char *argv[])
ok1(strstr(output, argv[0])); ok1(strstr(output, argv[0]));
ok1(strstr(output, "[-a]")); ok1(strstr(output, "[-a]"));
free(output); free(output);
free(argv);
/* It exits without freeing usage string. */
free(last_allocation);
output = NULL; output = NULL;
} }
...@@ -377,6 +407,7 @@ int main(int argc, char *argv[]) ...@@ -377,6 +407,7 @@ int main(int argc, char *argv[])
ok1(!strcmp(output, ok1(!strcmp(output,
"thisprog: --garbage: unrecognized option\n")); "thisprog: --garbage: unrecognized option\n"));
free(output); free(output);
free(argv);
output = NULL; output = NULL;
} }
...@@ -386,18 +417,19 @@ int main(int argc, char *argv[]) ...@@ -386,18 +417,19 @@ int main(int argc, char *argv[])
reset_options(); reset_options();
opt_register_noarg("-a", opt_register_noarg("-a",
opt_usage_and_exit, "[args]", ""); opt_usage_and_exit, "[args]", "");
exitval = setjmp(exited);
if (exitval == 0) {
argc = 2; argc = 2;
argv = malloc(sizeof(argv[0]) * 3); argv = malloc(sizeof(argv[0]) * 3);
argv[0] = "thisprog"; argv[0] = "thisprog";
argv[1] = "--garbage"; argv[1] = "--garbage";
argv[2] = NULL; argv[2] = NULL;
exitval = setjmp(exited);
if (exitval == 0) {
opt_parse(&argc, argv, opt_log_stderr_exit); opt_parse(&argc, argv, opt_log_stderr_exit);
fail("opt_log_stderr_exit returned?"); fail("opt_log_stderr_exit returned?");
} else { } else {
ok1(exitval - 1 == 1); ok1(exitval - 1 == 1);
} }
free(argv);
ok1(!strcmp(output, ok1(!strcmp(output,
"thisprog: --garbage: unrecognized option\n")); "thisprog: --garbage: unrecognized option\n"));
free(output); free(output);
......
...@@ -25,6 +25,9 @@ int main(int argc, char *argv[]) ...@@ -25,6 +25,9 @@ int main(int argc, char *argv[])
ok1(strcmp(argv[1], "extra") == 0); ok1(strcmp(argv[1], "extra") == 0);
ok1(strcmp(argv[2], "args") == 0); ok1(strcmp(argv[2], "args") == 0);
/* parse_args allocates argv */
free(argv);
return exit_status(); return exit_status();
} }
...@@ -106,5 +106,7 @@ int main(int argc, char *argv[]) ...@@ -106,5 +106,7 @@ int main(int argc, char *argv[])
ok1(strstr(output, "AAAAll")); ok1(strstr(output, "AAAAll"));
free(output); free(output);
free(shortname);
free(longname);
return exit_status(); return exit_status();
} }
...@@ -291,5 +291,7 @@ int main(int argc, char *argv[]) ...@@ -291,5 +291,7 @@ int main(int argc, char *argv[])
ok1(strcmp(argv[4], "-a") == 0); ok1(strcmp(argv[4], "-a") == 0);
ok1(!argv[5]); ok1(!argv[5]);
/* parse_args allocates argv */
free(argv);
return exit_status(); return exit_status();
} }
...@@ -29,7 +29,7 @@ void show_arg(char buf[OPT_SHOW_LEN], const char *arg) ...@@ -29,7 +29,7 @@ void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
char *err_output = NULL; char *err_output = NULL;
static void save_err_output(const char *fmt, ...) void save_err_output(const char *fmt, ...)
{ {
va_list ap; va_list ap;
char *p; char *p;
...@@ -49,7 +49,8 @@ static void save_err_output(const char *fmt, ...) ...@@ -49,7 +49,8 @@ static void save_err_output(const char *fmt, ...)
err_output = p; err_output = p;
} }
/* FIXME: This leaks, BTW. */ static bool allocated = false;
bool parse_args(int *argc, char ***argv, ...) bool parse_args(int *argc, char ***argv, ...)
{ {
char **a; char **a;
...@@ -63,7 +64,12 @@ bool parse_args(int *argc, char ***argv, ...) ...@@ -63,7 +64,12 @@ bool parse_args(int *argc, char ***argv, ...)
(*argc)++; (*argc)++;
a = realloc(a, sizeof(*a) * (*argc + 1)); a = realloc(a, sizeof(*a) * (*argc + 1));
} }
if (allocated)
free(*argv);
*argv = a; *argv = a;
allocated = true;
/* Re-set before parsing. */ /* Re-set before parsing. */
optind = 0; optind = 0;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
bool parse_args(int *argc, char ***argv, ...); bool parse_args(int *argc, char ***argv, ...);
extern char *err_output; extern char *err_output;
void save_err_output(const char *fmt, ...);
extern unsigned int test_cb_called; extern unsigned int test_cb_called;
char *test_noarg(void *arg); char *test_noarg(void *arg);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment