From a7daeb2a12ab0c2159fcdac5fd1d05dac739ed8f Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Fri, 17 May 2019 09:25:57 +1000 Subject: [PATCH] [c-host] option: fix memory corruption due to usage of old pointers --- VERSION | 2 +- common/include/common/option.h | 6 +++--- common/src/option.c | 17 ++++++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/VERSION b/VERSION index b8e843c0..db13ef05 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -a12-186-gb662128708+1 \ No newline at end of file +a12-187-g2fe9dc7ca1+1 \ No newline at end of file diff --git a/common/include/common/option.h b/common/include/common/option.h index f001ddcb..df73aee6 100644 --- a/common/include/common/option.h +++ b/common/include/common/option.h @@ -33,9 +33,9 @@ struct Option; struct Option { - const char * module; - const char * name; - const char * description; + char * module; + char * name; + char * description; enum OptionType type; union diff --git a/common/src/option.c b/common/src/option.c index 07c259cd..cbb0a5f8 100644 --- a/common/src/option.c +++ b/common/src/option.c @@ -37,7 +37,7 @@ struct OptionGroup struct State { bool doHelp; - struct Option * options; + struct Option ** options; int oCount; struct OptionGroup * groups; int gCount; @@ -101,12 +101,13 @@ bool option_register(struct Option options[]) state.options = realloc( 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) { - 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)); if (!o->parser) @@ -177,6 +178,7 @@ bool option_register(struct Option options[]) group->pad = len; ++group->count; + break; } if (!found) @@ -205,9 +207,10 @@ void option_free() { 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) free(o->value.x_string); + free(o); } free(state.options); state.options = NULL; @@ -256,7 +259,7 @@ bool option_parse(int argc, char * argv[]) struct Option * o; 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)) continue; @@ -460,7 +463,7 @@ bool option_validate() bool ok = true; for(int i = 0; i < state.oCount; ++i) { - struct Option * o = &state.options[i]; + struct Option * o = state.options[i]; const char * error = NULL; 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) { - struct Option * o = &state.options[i]; + struct Option * o = state.options[i]; if ((strcmp(o->module, module) == 0) && (strcmp(o->name, name) == 0)) return o; }