]> git.ozlabs.org Git - ccan/commitdiff
opt: don't leak on exit (valgrind complains).
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 15 Dec 2011 03:45:47 +0000 (14:15 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 15 Dec 2011 03:45:47 +0000 (14:15 +1030)
Recent real usage case showed this leak when we call opt_usage_and_exit:
we don't bother freeing before exit.  With valgrind, it matters.

ccan/opt/helpers.c
ccan/opt/test/run-helpers.c

index 420a97a4fcac596fd31999ffbda42bb229a7f0f3..17d56e0201dc66d7810b26f1cd40a1e5e702f0a7 100644 (file)
@@ -123,12 +123,18 @@ char *opt_inc_intval(int *i)
 char *opt_version_and_exit(const char *version)
 {
        printf("%s\n", version);
 char *opt_version_and_exit(const char *version)
 {
        printf("%s\n", version);
+       /* Don't have valgrind complain! */
+       opt_free_table();
        exit(0);
 }
 
 char *opt_usage_and_exit(const char *extra)
 {
        exit(0);
 }
 
 char *opt_usage_and_exit(const char *extra)
 {
-       printf("%s", opt_usage(opt_argv0, extra));
+       char *usage = opt_usage(opt_argv0, extra);
+       printf("%s", usage);
+       /* Don't have valgrind complain! */
+       free(usage);
+       opt_free_table();
        exit(0);
 }
 
        exit(0);
 }
 
index 589962fe4d8fadaffe6dc4cafd2764b2b5838a22..f85c73c4e9f10ad8e2d8e8ea9c8c4a5d2f25dfff 100644 (file)
@@ -19,9 +19,6 @@ static int saved_fprintf(FILE *ignored, const char *fmt, ...);
 #define vfprintf(f, fmt, ap) saved_vprintf(fmt, ap)
 static int saved_vprintf(const char *fmt, va_list ap);
 
 #define vfprintf(f, fmt, ap) saved_vprintf(fmt, 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/opt.c>
 #include <ccan/opt/usage.c>
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/opt.c>
 #include <ccan/opt/usage.c>
@@ -65,13 +62,6 @@ 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);
-}
-
 static void set_args(int *argc, char ***argv, ...)
 {
        va_list ap;
 static void set_args(int *argc, char ***argv, ...)
 {
        va_list ap;
@@ -87,7 +77,7 @@ static void set_args(int *argc, char ***argv, ...)
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
-       plan_tests(452);
+       plan_tests(454);
 
        /* opt_set_bool */
        {
 
        /* opt_set_bool */
        {
@@ -1005,6 +995,8 @@ int main(int argc, char *argv[])
                        fail("opt_show_version_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
                        fail("opt_show_version_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
+                       /* We should have freed table!. */
+                       ok1(opt_table == NULL);
                }
                ok1(strcmp(output, "1.2.3\n") == 0);
                free(output);
                }
                ok1(strcmp(output, "1.2.3\n") == 0);
                free(output);
@@ -1027,14 +1019,14 @@ int main(int argc, char *argv[])
                        fail("opt_usage_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
                        fail("opt_usage_and_exit returned?");
                } else {
                        ok1(exitval - 1 == 0);
+                       /* We should have freed table!. */
+                       ok1(opt_table == NULL);
                }
                ok1(strstr(output, "[args]"));
                ok1(strstr(output, argv[0]));
                ok1(strstr(output, "[-a]"));
                free(output);
                free(argv);
                }
                ok1(strstr(output, "[args]"));
                ok1(strstr(output, argv[0]));
                ok1(strstr(output, "[-a]"));
                free(output);
                free(argv);
-               /* It exits without freeing usage string. */
-               free(last_allocation);
                output = NULL;
        }
 
                output = NULL;
        }