Fix option string allocated/non allocated storage.

This commit is contained in:
Dale Weiler 2013-10-16 20:14:49 -04:00
parent f53502c9ca
commit b147602d78
3 changed files with 28 additions and 16 deletions

28
gmqcc.h
View file

@ -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 */

12
main.c
View file

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

4
opts.c
View file

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