settings_data: Guard against possible realloc/malloc failures

This commit is contained in:
Lioncash 2014-09-11 20:10:43 -04:00
parent 857457a615
commit 8cf195868e
3 changed files with 62 additions and 6 deletions

View File

@ -77,7 +77,8 @@ static int menu_info_screen_iterate(unsigned action)
driver.menu->selection_ptr,
setting_data_get_mainmenu(true))))
{
strlcpy(needle, current_setting->name, sizeof(needle));
if (current_setting)
strlcpy(needle, current_setting->name, sizeof(needle));
}
else
{

View File

@ -174,9 +174,9 @@ int menu_entries_push_list(menu_handle_t *menu,
if (!strcmp(label, "mainmenu"))
{
rarch_setting_t *setting;
setting_data = (rarch_setting_t *)setting_data_get_mainmenu(true);
rarch_setting_t *setting = (rarch_setting_t*)setting_data_find_setting(setting_data,
"Main Menu");
setting = (rarch_setting_t*)setting_data_find_setting(setting_data, "Main Menu");
file_list_clear(list);

View File

@ -19,6 +19,7 @@
#include "gfx/shader_common.h"
#include "input/input_common.h"
#include "config.def.h"
#include "retroarch_logger.h"
#ifdef APPLE
#include "input/apple_keycode.h"
@ -2169,7 +2170,28 @@ static void general_write_handler(const void *data)
rarch_main_command(rarch_cmd);
}
#define APPEND(VALUE) if (index == list_size) { list_size *= 2; list = (rarch_setting_t*)realloc(list, sizeof(rarch_setting_t) * list_size); } (list[index++]) = VALUE
#define APPEND(VALUE) \
if (index == list_size) \
{ \
rarch_setting_t* list_temp = NULL; \
\
list_size *= 2; \
list_temp = (rarch_setting_t*)realloc(list, sizeof(rarch_setting_t) * list_size); \
\
if (list_temp) \
{ \
list = list_temp; \
} \
else \
{ \
RARCH_ERR("Settings list reallocation failed.\n"); \
free(list); \
list = NULL; \
return NULL; \
} \
} \
(list[index++]) = VALUE
#define START_GROUP(NAME) { const char *GROUP_NAME = NAME; APPEND(setting_data_group_setting (ST_GROUP, NAME));
#define END_GROUP() APPEND(setting_data_group_setting (ST_END_GROUP, 0)); }
#define START_SUB_GROUP(NAME, GROUPNAME) { const char *SUBGROUP_NAME = NAME; (void)SUBGROUP_NAME; APPEND(setting_data_subgroup_setting (ST_SUB_GROUP, NAME, GROUPNAME));
@ -2208,6 +2230,7 @@ rarch_setting_t *setting_data_get_mainmenu(bool regenerate)
{
int index = 0;
static rarch_setting_t* list = NULL;
rarch_setting_t* list_tmp = NULL;
int list_size = 32;
static bool lists[32];
@ -2221,6 +2244,11 @@ rarch_setting_t *setting_data_get_mainmenu(bool regenerate)
}
list = (rarch_setting_t*)malloc(sizeof(rarch_setting_t) * list_size);
if (!list)
{
RARCH_ERR("setting_data_get_mainmenu list allocation failed.\n");
return NULL;
}
START_GROUP("Main Menu")
START_SUB_GROUP("State", GROUP_NAME)
@ -2269,7 +2297,18 @@ rarch_setting_t *setting_data_get_mainmenu(bool regenerate)
APPEND(terminator);
/* flatten this array to save ourselves some kilobytes */
list = (rarch_setting_t*)realloc(list, sizeof(rarch_setting_t) * index);
list_tmp = (rarch_setting_t*)realloc(list, sizeof(rarch_setting_t) * index);
if (list_tmp)
{
list = list_tmp;
}
else
{
RARCH_ERR("setting_data_get_mainmenu list flattening failed.\n");
free(list);
list = NULL;
}
/* do not optimize into return realloc(),
* list is static and must be written. */
return (rarch_setting_t*)list;
@ -2280,12 +2319,18 @@ rarch_setting_t *setting_data_get_list(void)
{
int i, player, index = 0;
static rarch_setting_t* list = NULL;
rarch_setting_t* list_tmp = NULL;
int list_size = 512;
if (list)
return list;
list = (rarch_setting_t*)malloc(sizeof(rarch_setting_t) * list_size);
if (!list)
{
RARCH_ERR("setting_data_get_list list allocation failed.\n");
return NULL;
}
START_GROUP("Driver Options")
START_SUB_GROUP("State", GROUP_NAME)
@ -2615,8 +2660,18 @@ rarch_setting_t *setting_data_get_list(void)
APPEND(terminator);
/* flatten this array to save ourselves some kilobytes. */
list = (rarch_setting_t*)
list_tmp = (rarch_setting_t*)
realloc(list, sizeof(rarch_setting_t) * index);
if (list_tmp)
{
list = list_tmp;
}
else
{
RARCH_ERR("setting_data_get_list list flattening failed.\n");
free(list);
list = NULL;
}
/* do not optimize into return realloc(),
* list is static and must be written. */