Commit ac9d55d8 authored by Rusty Russell's avatar Rusty Russell

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.
parent abde9907
...@@ -66,8 +66,8 @@ char *opt_set_intval(const char *arg, int *i) ...@@ -66,8 +66,8 @@ char *opt_set_intval(const char *arg, int *i)
if (err) if (err)
return err; return err;
*i = l; *i = l;
/* Beware truncation... */ /* Beware truncation, but don't generate untestable code. */
if (*i != l) if (sizeof(*i) != sizeof(l) && *i != l)
return arg_bad("value '%s' does not fit into an integer", arg); return arg_bad("value '%s' does not fit into an integer", arg);
return err; return err;
} }
......
...@@ -205,7 +205,8 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...)) ...@@ -205,7 +205,8 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
void opt_free_table(void) void opt_free_table(void)
{ {
free(opt_table); 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, ...)
......
...@@ -187,10 +187,12 @@ void opt_register_table(const struct opt_table *table, const char *desc); ...@@ -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 * This frees the internal memory and returns counters to zero. Call
* opt function. * 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);
......
...@@ -27,13 +27,6 @@ static void *saved_malloc(size_t size); ...@@ -27,13 +27,6 @@ static void *saved_malloc(size_t size);
#include <ccan/opt/usage.c> #include <ccan/opt/usage.c>
#include <ccan/opt/parse.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 char *output = NULL;
static int saved_vprintf(const char *fmt, va_list ap) static int saved_vprintf(const char *fmt, va_list ap)
......
...@@ -9,13 +9,6 @@ ...@@ -9,13 +9,6 @@
#include <ccan/opt/helpers.c> #include <ccan/opt/helpers.c>
#include <ccan/opt/parse.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. */ /* Test iterators. */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
......
...@@ -14,13 +14,6 @@ static char *my_cb(void *p) ...@@ -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. */ /* Test helpers. */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
......
...@@ -6,15 +6,6 @@ ...@@ -6,15 +6,6 @@
#include <ccan/opt/parse.c> #include <ccan/opt/parse.c>
#include "utils.h" #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[]) int main(int argc, char *argv[])
{ {
const char *myname = argv[0]; const char *myname = argv[0];
......
...@@ -49,6 +49,13 @@ void save_err_output(const char *fmt, ...) ...@@ -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; static bool allocated = false;
bool parse_args(int *argc, char ***argv, ...) bool parse_args(int *argc, char ***argv, ...)
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,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, ...); void save_err_output(const char *fmt, ...);
void reset_options(void);
extern unsigned int test_cb_called; extern unsigned int test_cb_called;
char *test_noarg(void *arg); char *test_noarg(void *arg);
......
...@@ -63,9 +63,6 @@ char *opt_usage(const char *argv0, const char *extra) ...@@ -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, "Usage: %s", argv0);
p += sprintf(p, " [-"); p += sprintf(p, " [-");
num = write_short_options(p); num = write_short_options(p);
......
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