Browse Source

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.
Rusty Russell 14 years ago
parent
commit
ac9d55d8c5

+ 2 - 2
ccan/opt/helpers.c

@@ -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... */
-	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 arg_bad("value '%s' does not fit into an integer", arg);
 	return err;
 	return err;
 }
 }

+ 2 - 1
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)
 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, ...)

+ 5 - 3
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, ...));
 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);
 
 

+ 0 - 7
ccan/opt/test/run-helpers.c

@@ -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)

+ 0 - 7
ccan/opt/test/run-iter.c

@@ -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[])
 {
 {

+ 0 - 7
ccan/opt/test/run-usage.c

@@ -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[])
 {
 {

+ 0 - 9
ccan/opt/test/run.c

@@ -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];

+ 7 - 0
ccan/opt/test/utils.c

@@ -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, ...)

+ 1 - 0
ccan/opt/test/utils.h

@@ -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);

+ 0 - 3
ccan/opt/usage.c

@@ -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);