[c-host] option: fix memory corruption due to usage of old pointers

This commit is contained in:
Geoffrey McRae 2019-05-17 09:25:57 +10:00
parent 2fe9dc7ca1
commit a7daeb2a12
3 changed files with 14 additions and 11 deletions

View File

@ -1 +1 @@
a12-186-gb662128708+1 a12-187-g2fe9dc7ca1+1

View File

@ -33,9 +33,9 @@ struct Option;
struct Option struct Option
{ {
const char * module; char * module;
const char * name; char * name;
const char * description; char * description;
enum OptionType type; enum OptionType type;
union union

View File

@ -37,7 +37,7 @@ struct OptionGroup
struct State struct State
{ {
bool doHelp; bool doHelp;
struct Option * options; struct Option ** options;
int oCount; int oCount;
struct OptionGroup * groups; struct OptionGroup * groups;
int gCount; int gCount;
@ -101,12 +101,13 @@ bool option_register(struct Option options[])
state.options = realloc( state.options = realloc(
state.options, state.options,
sizeof(struct Option) * (state.oCount + new) sizeof(struct Option *) * (state.oCount + new)
); );
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i) for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
{ {
struct Option * o = &state.options[state.oCount + i]; state.options[state.oCount + i] = (struct Option *)malloc(sizeof(struct Option));
struct Option * o = state.options[state.oCount + i];
memcpy(o, &options[i], sizeof(struct Option)); memcpy(o, &options[i], sizeof(struct Option));
if (!o->parser) if (!o->parser)
@ -177,6 +178,7 @@ bool option_register(struct Option options[])
group->pad = len; group->pad = len;
++group->count; ++group->count;
break;
} }
if (!found) if (!found)
@ -205,9 +207,10 @@ void option_free()
{ {
for(int i = 0; i < state.oCount; ++i) for(int i = 0; i < state.oCount; ++i)
{ {
struct Option * o = &state.options[i]; struct Option * o = state.options[i];
if (o->type == OPTION_TYPE_STRING) if (o->type == OPTION_TYPE_STRING)
free(o->value.x_string); free(o->value.x_string);
free(o);
} }
free(state.options); free(state.options);
state.options = NULL; state.options = NULL;
@ -256,7 +259,7 @@ bool option_parse(int argc, char * argv[])
struct Option * o; struct Option * o;
for(int i = 0; i < state.oCount; ++i) for(int i = 0; i < state.oCount; ++i)
{ {
o = &state.options[i]; o = state.options[i];
if ((strcmp(o->module, module) != 0) || (strcmp(o->name, name) != 0)) if ((strcmp(o->module, module) != 0) || (strcmp(o->name, name) != 0))
continue; continue;
@ -460,7 +463,7 @@ bool option_validate()
bool ok = true; bool ok = true;
for(int i = 0; i < state.oCount; ++i) for(int i = 0; i < state.oCount; ++i)
{ {
struct Option * o = &state.options[i]; struct Option * o = state.options[i];
const char * error = NULL; const char * error = NULL;
bool invalid = o->failed_set; bool invalid = o->failed_set;
@ -522,7 +525,7 @@ struct Option * option_get(const char * module, const char * name)
{ {
for(int i = 0; i < state.oCount; ++i) for(int i = 0; i < state.oCount; ++i)
{ {
struct Option * o = &state.options[i]; struct Option * o = state.options[i];
if ((strcmp(o->module, module) == 0) && (strcmp(o->name, name) == 0)) if ((strcmp(o->module, module) == 0) && (strcmp(o->name, name) == 0))
return o; return o;
} }