renamed OptionList items to options

fixed a heap buffer overflow
This commit is contained in:
Shaun Inman 2023-01-28 20:01:20 -05:00
parent efa1e9bd0c
commit 54e1590c0f

View file

@ -349,7 +349,7 @@ typedef struct OptionList OptionList;
typedef struct OptionList { typedef struct OptionList {
int count; int count;
int changed; int changed;
Option* items; Option* options;
// OptionList_callback_t on_set; // OptionList_callback_t on_set;
} OptionList; } OptionList;
@ -499,7 +499,7 @@ static struct {
} config = { } config = {
.frontend = (OptionList){ .frontend = (OptionList){
.count = FE_OPT_COUNT, .count = FE_OPT_COUNT,
.items = (Option[]){ .options = (Option[]){
[FE_OPT_SCANLINES] = { [FE_OPT_SCANLINES] = {
.key = "minarch_scanlines_grid", .key = "minarch_scanlines_grid",
.name = "Scanlines/Grid", .name = "Scanlines/Grid",
@ -611,16 +611,16 @@ static void Config_read(void) {
char key[256]; char key[256];
char value[256]; char value[256];
for (int i=0; config.frontend.items[i].key; i++) { for (int i=0; config.frontend.options[i].key; i++) {
Option* option = &config.frontend.items[i]; Option* option = &config.frontend.options[i];
Config_getValue(cfg, option->key, value); Config_getValue(cfg, option->key, value);
// TODO: handle not finding the expected value // TODO: handle not finding the expected value
OptionList_setOptionValue(&config.frontend, option->key, value); OptionList_setOptionValue(&config.frontend, option->key, value);
Config_syncFrontend(i, option->value); Config_syncFrontend(i, option->value);
} }
for (int i=0; config.core.items[i].key; i++) { for (int i=0; config.core.options[i].key; i++) {
Option* option = &config.core.items[i]; Option* option = &config.core.options[i];
Config_getValue(cfg, option->key, value); Config_getValue(cfg, option->key, value);
// TODO: handle not finding the expected value // TODO: handle not finding the expected value
OptionList_setOptionValue(&config.core, option->key, value); OptionList_setOptionValue(&config.core, option->key, value);
@ -679,12 +679,12 @@ static void Config_write(int override) {
FILE *file = fopen(path, "wb"); FILE *file = fopen(path, "wb");
if (!file) return; if (!file) return;
for (int i=0; config.frontend.items[i].key; i++) { for (int i=0; config.frontend.options[i].key; i++) {
Option* option = &config.frontend.items[i]; Option* option = &config.frontend.options[i];
fprintf(file, "%s = %s\n", option->key, option->values[option->value]); fprintf(file, "%s = %s\n", option->key, option->values[option->value]);
} }
for (int i=0; config.core.items[i].key; i++) { for (int i=0; config.core.options[i].key; i++) {
Option* option = &config.core.items[i]; Option* option = &config.core.options[i];
fprintf(file, "%s = %s\n", option->key, option->values[option->value]); fprintf(file, "%s = %s\n", option->key, option->values[option->value]);
} }
for (int i=0; config.controls[i].name; i++) { for (int i=0; config.controls[i].name; i++) {
@ -715,13 +715,13 @@ static void Config_restore(void) {
} }
config.loaded = CONFIG_NONE; config.loaded = CONFIG_NONE;
for (int i=0; config.frontend.items[i].key; i++) { for (int i=0; config.frontend.options[i].key; i++) {
Option* option = &config.frontend.items[i]; Option* option = &config.frontend.options[i];
option->value = option->default_value; option->value = option->default_value;
Config_syncFrontend(i, option->value); Config_syncFrontend(i, option->value);
} }
for (int i=0; config.core.items[i].key; i++) { for (int i=0; config.core.options[i].key; i++) {
Option* option = &config.core.items[i]; Option* option = &config.core.options[i];
option->value = option->default_value; option->value = option->default_value;
} }
config.core.changed = 1; // let the core know config.core.changed = 1; // let the core know
@ -765,12 +765,12 @@ static void OptionList_init(const struct retro_core_option_definition *defs) {
config.core.count = count; config.core.count = count;
if (count) { if (count) {
config.core.items = calloc(count, sizeof(Option)); config.core.options = calloc(count+1, sizeof(Option));
for (int i=0; i<config.core.count; i++) { for (int i=0; i<config.core.count; i++) {
int len; int len;
const struct retro_core_option_definition *def = &defs[i]; const struct retro_core_option_definition *def = &defs[i];
Option* item = &config.core.items[i]; Option* item = &config.core.options[i];
len = strlen(def->key) + 1; len = strlen(def->key) + 1;
item->key = calloc(len, sizeof(char)); item->key = calloc(len, sizeof(char));
@ -843,12 +843,12 @@ static void OptionList_vars(const struct retro_variable *vars) {
config.core.count = count; config.core.count = count;
if (count) { if (count) {
config.core.items = calloc(count, sizeof(Option)); config.core.options = calloc(count+1, sizeof(Option));
for (int i=0; i<config.core.count; i++) { for (int i=0; i<config.core.count; i++) {
int len; int len;
const struct retro_variable *var = &vars[i]; const struct retro_variable *var = &vars[i];
Option* item = &config.core.items[i]; Option* item = &config.core.options[i];
len = strlen(var->key) + 1; len = strlen(var->key) + 1;
item->key = calloc(len, sizeof(char)); item->key = calloc(len, sizeof(char));
@ -910,7 +910,7 @@ static void OptionList_reset(void) {
if (!config.core.count) return; if (!config.core.count) return;
for (int i=0; i<config.core.count; i++) { for (int i=0; i<config.core.count; i++) {
Option* item = &config.core.items[i]; Option* item = &config.core.options[i];
if (item->var) { if (item->var) {
// values/labels are all points to var // values/labels are all points to var
// so no need to free individually // so no need to free individually
@ -930,12 +930,12 @@ static void OptionList_reset(void) {
free(item->key); free(item->key);
free(item->name); free(item->name);
} }
free(config.core.items); free(config.core.options);
} }
static Option* OptionList_getOption(OptionList* list, const char* key) { static Option* OptionList_getOption(OptionList* list, const char* key) {
for (int i=0; i<list->count; i++) { for (int i=0; i<list->count; i++) {
Option* item = &list->items[i]; Option* item = &list->options[i];
if (!strcmp(item->key, key)) return item; if (!strcmp(item->key, key)) return item;
} }
return NULL; return NULL;
@ -992,7 +992,7 @@ static void input_poll_callback(void) {
// TODO: tmp? // TODO: tmp?
if ((PAD_isPressed(BTN_L2) && PAD_justPressed(BTN_R2)) || PAD_isPressed(BTN_R2) && PAD_justPressed(BTN_L2)) { if ((PAD_isPressed(BTN_L2) && PAD_justPressed(BTN_R2)) || PAD_isPressed(BTN_R2) && PAD_justPressed(BTN_L2)) {
show_debug = !show_debug; show_debug = !show_debug;
config.frontend.items[FE_OPT_DEBUG].value = show_debug; // TODO: standardize this for all config.frontend? config.frontend.options[FE_OPT_DEBUG].value = show_debug; // TODO: standardize this for all config.frontend?
} }
// TODO: test fast_forward once implemented // TODO: test fast_forward once implemented
@ -2432,7 +2432,7 @@ static int Menu_options(MenuList* list);
static int options_frontend_change(MenuList* list, int i) { static int options_frontend_change(MenuList* list, int i) {
MenuItem* item = &list->items[i]; MenuItem* item = &list->items[i];
Option* option = &config.frontend.items[i]; Option* option = &config.frontend.options[i];
LOG_info("%s (%s) changed from `%s` (%s) to `%s` (%s)\n", item->name, item->key, LOG_info("%s (%s) changed from `%s` (%s) to `%s` (%s)\n", item->name, item->key,
item->values[option->value], option->values[option->value], item->values[option->value], option->values[option->value],
item->values[item->value], option->values[item->value] item->values[item->value], option->values[item->value]
@ -2450,7 +2450,7 @@ static int options_frontend_open(MenuList* list, int i) {
// TODO: where do I free this? // TODO: where do I free this?
options_frontend_menu.items = calloc(config.frontend.count+1, sizeof(MenuItem)); options_frontend_menu.items = calloc(config.frontend.count+1, sizeof(MenuItem));
for (int j=0; j<config.frontend.count; j++) { for (int j=0; j<config.frontend.count; j++) {
Option* option = &config.frontend.items[j]; Option* option = &config.frontend.options[j];
MenuItem* item = &options_frontend_menu.items[j]; MenuItem* item = &options_frontend_menu.items[j];
item->key = option->key; item->key = option->key;
item->name = option->name; item->name = option->name;
@ -2462,7 +2462,7 @@ static int options_frontend_open(MenuList* list, int i) {
else { else {
// update values // update values
for (int j=0; j<config.frontend.count; j++) { for (int j=0; j<config.frontend.count; j++) {
Option* option = &config.frontend.items[j]; Option* option = &config.frontend.options[j];
MenuItem* item = &options_frontend_menu.items[j]; MenuItem* item = &options_frontend_menu.items[j];
item->value = option->value; item->value = option->value;
} }
@ -2491,12 +2491,12 @@ static int options_emulator_open(MenuList* list, int i) {
// TODO: where do I free this? // TODO: where do I free this?
options_emulator_menu.items = calloc(config.core.count+1, sizeof(MenuItem)); options_emulator_menu.items = calloc(config.core.count+1, sizeof(MenuItem));
for (int j=0; j<config.core.count; j++) { for (int j=0; j<config.core.count; j++) {
Option* option = &config.core.items[j]; Option* option = &config.core.options[j];
MenuItem* item = &options_emulator_menu.items[j]; MenuItem* item = &options_emulator_menu.items[j];
item->key = option->key; item->key = option->key;
item->name = option->name; item->name = option->name;
item->desc = NULL; // gambatte crashes if this isn't set to NULL :thinking_face: item->desc = NULL; // gambatte crashes if this isn't set to NULL :thinking_face:
// item->desc = option->desc; // TODO: these need to be copyfit/truncated item->desc = option->desc; // TODO: these need to be copyfit/truncated
item->value = option->value; item->value = option->value;
item->values = option->labels; item->values = option->labels;
} }
@ -2504,7 +2504,7 @@ static int options_emulator_open(MenuList* list, int i) {
else { else {
// update values // update values
for (int j=0; j<config.core.count; j++) { for (int j=0; j<config.core.count; j++) {
Option* option = &config.core.items[j]; Option* option = &config.core.options[j];
MenuItem* item = &options_emulator_menu.items[j]; MenuItem* item = &options_emulator_menu.items[j];
item->value = option->value; item->value = option->value;
} }
@ -3091,8 +3091,8 @@ static int Menu_options(MenuList* list) {
else GFX_sync(); else GFX_sync();
} }
GFX_clearAll(); // GFX_clearAll();
GFX_flip(screen); // GFX_flip(screen);
return 0; return 0;
} }
@ -3308,7 +3308,7 @@ static void Menu_loop(void) {
int max_width = SCREEN_WIDTH - SCALE1(PADDING * 2) - ow; int max_width = SCREEN_WIDTH - SCALE1(PADDING * 2) - ow;
char display_name[256]; char display_name[256];
int text_width = GFX_truncateDisplayName(rom_name, display_name, max_width); int text_width = GFX_truncateText(font.large, rom_name, display_name, max_width);
max_width = MIN(max_width, text_width); max_width = MIN(max_width, text_width);
SDL_Surface* text; SDL_Surface* text;