From b147602d78c28f73627a617a00e49e4eb6f6b3f8 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Wed, 16 Oct 2013 20:14:49 -0400 Subject: [PATCH] Fix option string allocated/non allocated storage. --- gmqcc.h | 28 +++++++++++++++++++--------- main.c | 12 +++++++----- opts.c | 4 ++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/gmqcc.h b/gmqcc.h index 7ebb1c6..7ad9a28 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -1012,11 +1012,19 @@ typedef enum { COMPILER_GMQCC /* this QuakeC */ } opts_std_t; -typedef union { - bool B; - uint16_t U16; - uint32_t U32; - char *STR; +typedef struct { + union { + bool b; + uint16_t u16; + uint32_t u32; + + union { + char *p; + const char *c; + } str; + } data; + + bool allocated; } opt_value_t; @@ -1038,9 +1046,11 @@ extern opts_cmd_t opts; #define OPTS_WARN(i) OPTS_GENERIC(opts.warn, (i)) #define OPTS_WERROR(i) OPTS_GENERIC(opts.werror, (i)) #define OPTS_OPTIMIZATION(i) OPTS_GENERIC(opts.optimization, (i)) -#define OPTS_OPTION_BOOL(X) (opts.options[X].B) -#define OPTS_OPTION_U16(X) (opts.options[X].U16) -#define OPTS_OPTION_U32(X) (opts.options[X].U32) -#define OPTS_OPTION_STR(X) (opts.options[X].STR) +#define OPTS_OPTION_DUPED(X) (opts.options[X].allocated) +#define OPTS_OPTION_BOOL(X) (opts.options[X].data.b) +#define OPTS_OPTION_U16(X) (opts.options[X].data.u16) +#define OPTS_OPTION_U32(X) (opts.options[X].data.u32) +#define OPTS_OPTION_DUP(X) *(OPTS_OPTION_DUPED(X)=true, &(opts.options[X].data.str.p)) +#define OPTS_OPTION_STR(X) (opts.options[X].data.str.c) #endif /*! GMQCC_HDR */ diff --git a/main.c b/main.c index f9a71e2..78293d6 100644 --- a/main.c +++ b/main.c @@ -557,7 +557,6 @@ static bool progs_nextline(char **out, size_t *alen, fs_file_t *src) { int main(int argc, char **argv) { size_t itr; int retval = 0; - bool opts_output_free = false; bool operators_free = false; bool progs_src = false; fs_file_t *outfile = NULL; @@ -690,8 +689,7 @@ int main(int argc, char **argv) { item.type = TYPE_QC; vec_push(items, item); } else if (!opts_output_wasset) { - OPTS_OPTION_STR(OPTION_OUTPUT) = util_strdup(line); - opts_output_free = true; + OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line); hasline = true; } } @@ -780,8 +778,12 @@ cleanup: if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) if(parser) parser_cleanup(parser); - if (opts_output_free) - mem_d(OPTS_OPTION_STR(OPTION_OUTPUT)); + + /* free allocated option strings */ + for (itr = 0; itr < OPTION_COUNT; itr++) + if (OPTS_OPTION_DUPED(itr)) + mem_d(OPTS_OPTION_STR(itr)); + if (operators_free) mem_d((void*)operators); diff --git a/opts.c b/opts.c index 23a0f4c..e50bd74 100644 --- a/opts.c +++ b/opts.c @@ -60,7 +60,7 @@ opts_cmd_t opts; /* command line options */ static void opts_setdefault(void) { memset(&opts, 0, sizeof(opts_cmd_t)); OPTS_OPTION_BOOL(OPTION_CORRECTION) = true; - OPTS_OPTION_STR(OPTION_PROGSRC) = (char*)"progs.src"; + OPTS_OPTION_STR(OPTION_PROGSRC) = "progs.src"; /* warnings */ opts_set(opts.warn, WARN_UNUSED_VARIABLE, true); @@ -129,7 +129,7 @@ void opts_restore_non_Werror_all() { void opts_init(const char *output, int standard, size_t arraysize) { opts_setdefault(); - OPTS_OPTION_STR(OPTION_OUTPUT) = (char*)output; + OPTS_OPTION_STR(OPTION_OUTPUT) = output; OPTS_OPTION_U32(OPTION_STANDARD) = standard; OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE) = arraysize; OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = 16; -- 2.39.2