]> git.ozlabs.org Git - ccan/commitdiff
opt: complete coverage, enhance opt_free_table.
authorRusty Russell <rusty@rustcorp.com.au>
Sun, 14 Aug 2011 01:04:44 +0000 (10:34 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Sun, 14 Aug 2011 01:04:44 +0000 (10:34 +0930)
No point checking malloc failure in usage(), since we don't elsewhere.
We get 100% coverage with -O (due to code elimination) or 64 bit.

ccan/opt/helpers.c
ccan/opt/opt.c
ccan/opt/opt.h
ccan/opt/test/run-helpers.c
ccan/opt/test/run-iter.c
ccan/opt/test/run-usage.c
ccan/opt/test/run.c
ccan/opt/test/utils.c
ccan/opt/test/utils.h
ccan/opt/usage.c

index 53fb0158827aca291e5ce39ff5566da60f49c8ec..dfeb4e2e790a553173830d039ae84b8026df2fa8 100644 (file)
@@ -66,8 +66,8 @@ char *opt_set_intval(const char *arg, int *i)
        if (err)
                return err;
        *i = l;
        if (err)
                return err;
        *i = l;
-       /* Beware truncation... */
-       if (*i != l)
+       /* Beware truncation, but don't generate untestable code. */
+       if (sizeof(*i) != sizeof(l) && *i != l)
                return arg_bad("value '%s' does not fit into an integer", arg);
        return err;
 }
                return arg_bad("value '%s' does not fit into an integer", arg);
        return err;
 }
index 5aaa000b64a1cd31fd1078c021d62c4117c47146..db7686be10eba44a08cec27c945aff7002a5b0fb 100644 (file)
@@ -205,7 +205,8 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
 void opt_free_table(void)
 {
        free(opt_table);
 void opt_free_table(void)
 {
        free(opt_table);
-       opt_table=0;
+       opt_table = NULL;
+       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
 }
 
 void opt_log_stderr(const char *fmt, ...)
 }
 
 void opt_log_stderr(const char *fmt, ...)
index 550c342d7b190249adbcb9b89f4384b3522aaf4a..55a78d8ff139e9cbe1de384d4d1da0021557e3aa 100644 (file)
@@ -187,10 +187,12 @@ void opt_register_table(const struct opt_table *table, const char *desc);
 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
 
 /**
 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
 
 /**
- * opt_free_table - free the table.
+ * opt_free_table - reset the opt library.
  *
  *
- * This frees the internal memory. Call this as the last
- * opt function.
+ * This frees the internal memory and returns counters to zero.  Call
+ * this as the last opt function to avoid memory leaks.  You can also
+ * use this function to reset option handling to its initial state (no
+ * options registered).
  */
 void opt_free_table(void);
 
  */
 void opt_free_table(void);
 
index e0fbd32e6bcf6f6a28c17e9818a169ff22997747..8cf229cc6acc230fd6e32bd794938f24c967d141 100644 (file)
@@ -27,13 +27,6 @@ static void *saved_malloc(size_t size);
 #include <ccan/opt/usage.c>
 #include <ccan/opt/parse.c>
 
 #include <ccan/opt/usage.c>
 #include <ccan/opt/parse.c>
 
-static void reset_options(void)
-{
-       free(opt_table);
-       opt_table = NULL;
-       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
-}
-
 static char *output = NULL;
 
 static int saved_vprintf(const char *fmt, va_list ap)
 static char *output = NULL;
 
 static int saved_vprintf(const char *fmt, va_list ap)
index 66fd5db8b0ac8533842a9d03f1a68fd1e99ffe6b..e6b13dadea8d33b38d02e55ca99c20e0d956d1d8 100644 (file)
@@ -9,13 +9,6 @@
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/parse.c>
 
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/parse.c>
 
-static void reset_options(void)
-{
-       free(opt_table);
-       opt_table = NULL;
-       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
-}
-
 /* Test iterators. */
 int main(int argc, char *argv[])
 {
 /* Test iterators. */
 int main(int argc, char *argv[])
 {
index 2af2c7eebab773c770f21f7ae4a63bce81cfe97e..fa1dc64c112fe46dc36e02cc4b140de50681372c 100644 (file)
@@ -14,13 +14,6 @@ static char *my_cb(void *p)
        return NULL;
 }
 
        return NULL;
 }
 
-static void reset_options(void)
-{
-       free(opt_table);
-       opt_table = NULL;
-       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
-}
-
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
 /* Test helpers. */
 int main(int argc, char *argv[])
 {
index 9a769bad12d1a5dca1d74a1db2db1c2e16fd57b3..0bf043c1912a05695402ee1e3ceafb4fcf496510 100644 (file)
@@ -6,15 +6,6 @@
 #include <ccan/opt/parse.c>
 #include "utils.h"
 
 #include <ccan/opt/parse.c>
 #include "utils.h"
 
-static void reset_options(void)
-{
-       free(opt_table);
-       opt_table = NULL;
-       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
-       free(err_output);
-       err_output = NULL;
-}
-
 int main(int argc, char *argv[])
 {
        const char *myname = argv[0];
 int main(int argc, char *argv[])
 {
        const char *myname = argv[0];
index 9544fa77bd3493382c07e72f77f770d09b3d4cc7..21e0b727ebd6cab4762526dc5a21a11a3eb5f072 100644 (file)
@@ -49,6 +49,13 @@ void save_err_output(const char *fmt, ...)
                err_output = p;
 }      
 
                err_output = p;
 }      
 
+void reset_options(void)
+{
+       opt_free_table();
+       free(err_output);
+       err_output = NULL;
+}
+
 static bool allocated = false;
 
 bool parse_args(int *argc, char ***argv, ...)
 static bool allocated = false;
 
 bool parse_args(int *argc, char ***argv, ...)
index f7c18967abc946615b309c6a573282fccb5576a6..33a2dbdc5e5ec1dc16ef8f3e9d531ba1cc5704b5 100644 (file)
@@ -6,6 +6,7 @@
 bool parse_args(int *argc, char ***argv, ...);
 extern char *err_output;
 void save_err_output(const char *fmt, ...);
 bool parse_args(int *argc, char ***argv, ...);
 extern char *err_output;
 void save_err_output(const char *fmt, ...);
+void reset_options(void);
 
 extern unsigned int test_cb_called;
 char *test_noarg(void *arg);
 
 extern unsigned int test_cb_called;
 char *test_noarg(void *arg);
index f321ed2949a8bfb7cd5cdbce5955780dba1cd233..873ee5db1856cf0bf326a8ed4ba9f19aa22c2db6 100644 (file)
@@ -63,9 +63,6 @@ char *opt_usage(const char *argv0, const char *extra)
        }
 
        p = ret = malloc(len);
        }
 
        p = ret = malloc(len);
-       if (!ret)
-               return NULL;
-
        p += sprintf(p, "Usage: %s", argv0);
        p += sprintf(p, " [-");
        num = write_short_options(p);
        p += sprintf(p, "Usage: %s", argv0);
        p += sprintf(p, " [-");
        num = write_short_options(p);