From: Rusty Russell Date: Sun, 14 Aug 2011 01:04:44 +0000 (+0930) Subject: opt: complete coverage, enhance opt_free_table. X-Git-Url: https://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=ac9d55d8c5af9697be8c4dd4f27de61e3cb8bf95;ds=sidebyside opt: complete coverage, enhance opt_free_table. 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. --- diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c index 53fb0158..dfeb4e2e 100644 --- a/ccan/opt/helpers.c +++ b/ccan/opt/helpers.c @@ -66,8 +66,8 @@ char *opt_set_intval(const char *arg, int *i) 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; } diff --git a/ccan/opt/opt.c b/ccan/opt/opt.c index 5aaa000b..db7686be 100644 --- a/ccan/opt/opt.c +++ b/ccan/opt/opt.c @@ -205,7 +205,8 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) 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, ...) diff --git a/ccan/opt/opt.h b/ccan/opt/opt.h index 550c342d..55a78d8f 100644 --- a/ccan/opt/opt.h +++ b/ccan/opt/opt.h @@ -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, ...)); /** - * 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); diff --git a/ccan/opt/test/run-helpers.c b/ccan/opt/test/run-helpers.c index e0fbd32e..8cf229cc 100644 --- a/ccan/opt/test/run-helpers.c +++ b/ccan/opt/test/run-helpers.c @@ -27,13 +27,6 @@ static void *saved_malloc(size_t size); #include #include -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) diff --git a/ccan/opt/test/run-iter.c b/ccan/opt/test/run-iter.c index 66fd5db8..e6b13dad 100644 --- a/ccan/opt/test/run-iter.c +++ b/ccan/opt/test/run-iter.c @@ -9,13 +9,6 @@ #include #include -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[]) { diff --git a/ccan/opt/test/run-usage.c b/ccan/opt/test/run-usage.c index 2af2c7ee..fa1dc64c 100644 --- a/ccan/opt/test/run-usage.c +++ b/ccan/opt/test/run-usage.c @@ -14,13 +14,6 @@ static char *my_cb(void *p) 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[]) { diff --git a/ccan/opt/test/run.c b/ccan/opt/test/run.c index 9a769bad..0bf043c1 100644 --- a/ccan/opt/test/run.c +++ b/ccan/opt/test/run.c @@ -6,15 +6,6 @@ #include #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]; diff --git a/ccan/opt/test/utils.c b/ccan/opt/test/utils.c index 9544fa77..21e0b727 100644 --- a/ccan/opt/test/utils.c +++ b/ccan/opt/test/utils.c @@ -49,6 +49,13 @@ void save_err_output(const char *fmt, ...) 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, ...) diff --git a/ccan/opt/test/utils.h b/ccan/opt/test/utils.h index f7c18967..33a2dbdc 100644 --- a/ccan/opt/test/utils.h +++ b/ccan/opt/test/utils.h @@ -6,6 +6,7 @@ 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); diff --git a/ccan/opt/usage.c b/ccan/opt/usage.c index f321ed29..873ee5db 100644 --- a/ccan/opt/usage.c +++ b/ccan/opt/usage.c @@ -63,9 +63,6 @@ char *opt_usage(const char *argv0, const char *extra) } p = ret = malloc(len); - if (!ret) - return NULL; - p += sprintf(p, "Usage: %s", argv0); p += sprintf(p, " [-"); num = write_short_options(p);