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.
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;
}
void opt_free_table(void)
{
free(opt_table);
void opt_free_table(void)
{
free(opt_table);
+ 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, ...)
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);
#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)
#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[])
{
-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[])
{
#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];
+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, ...)
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);
- 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);