Skip to content

Commit

Permalink
* Cut down on strlcat calls when possible and replace them with clever
Browse files Browse the repository at this point in the history
usage of strlcpy (when position/offset of previous strlcpy/snprintf call
is known. strlcat implementation in libretro-common makes implicit strlen
call, using strlcpy avoids this
* Reduce a bunch of local char variables by use of said clever usage,
should save up on local stack size usage
  • Loading branch information
LibretroAdmin committed Jun 18, 2023
1 parent dbc1a41 commit 631301b
Show file tree
Hide file tree
Showing 23 changed files with 345 additions and 361 deletions.
33 changes: 15 additions & 18 deletions cheat_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,37 +193,34 @@ bool cheat_manager_save(

for (i = 0; i < cheat_st->size; i++)
{
size_t _len;
unsigned j;
char endian_key[100];
char key[256];
char desc_key[128];
char code_key[128];
char enable_key[128];
char formatted_number[12];
char var_key[128];
char key[256];

formatted_number[0] = '\0';
snprintf(formatted_number, sizeof(formatted_number), "cheat%u_", i);

strlcpy(endian_key, formatted_number, sizeof(endian_key));
strlcat(endian_key, "big_endian", sizeof(endian_key));
strlcpy(desc_key, formatted_number, sizeof(desc_key));
strlcat(desc_key, "desc", sizeof(desc_key));
strlcpy(code_key, formatted_number, sizeof(code_key));
strlcat(code_key, "code", sizeof(code_key));
strlcpy(enable_key, formatted_number, sizeof(enable_key));
strlcat(enable_key, "enable", sizeof(enable_key));
_len = strlcpy(var_key, formatted_number, sizeof(var_key));

strlcpy(var_key + _len, "desc", sizeof(var_key) - _len);
if (!string_is_empty(cheat_st->cheats[i].desc))
config_set_string(conf, desc_key, cheat_st->cheats[i].desc);
config_set_string(conf, var_key, cheat_st->cheats[i].desc);
else
config_set_string(conf, desc_key, cheat_st->cheats[i].code);
config_set_string(conf, var_key, cheat_st->cheats[i].code);

config_set_string(conf, code_key, cheat_st->cheats[i].code);
config_set_string(conf, enable_key,
strlcpy(var_key + _len, "code", sizeof(var_key) - _len);
config_set_string(conf, var_key, cheat_st->cheats[i].code);

strlcpy(var_key + _len, "enable", sizeof(var_key) - _len);
config_set_string(conf, var_key,
cheat_st->cheats[i].state
? "true"
: "false");
config_set_string(conf, endian_key,

strlcpy(var_key + _len, "big_endian", sizeof(var_key) - _len);
config_set_string(conf, var_key,
cheat_st->cheats[i].big_endian
? "true"
: "false"
Expand Down
14 changes: 8 additions & 6 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,7 @@ bool command_event_save_auto_state(
bool savestate_auto_save,
const enum rarch_core_type current_core_type)
{
size_t _len;
runloop_state_t *runloop_st = runloop_state_get_ptr();
char savestate_name_auto[PATH_MAX_LENGTH];

Expand All @@ -1271,12 +1272,12 @@ bool command_event_save_auto_state(
if (string_is_empty(path_basename(path_get(RARCH_PATH_BASENAME))))
return false;

strlcpy(savestate_name_auto,
_len = strlcpy(savestate_name_auto,
runloop_st->name.savestate,
sizeof(savestate_name_auto));
strlcat(savestate_name_auto,
strlcpy(savestate_name_auto + _len,
".auto",
sizeof(savestate_name_auto));
sizeof(savestate_name_auto) - _len);

if (content_save_state((const char*)savestate_name_auto, true, true))
RARCH_LOG("%s \"%s\" %s.\n",
Expand Down Expand Up @@ -1367,6 +1368,7 @@ bool command_event_load_entry_state(settings_t *settings)

void command_event_load_auto_state(void)
{
size_t _len;
char savestate_name_auto[PATH_MAX_LENGTH];
runloop_state_t *runloop_st = runloop_state_get_ptr();

Expand All @@ -1382,12 +1384,12 @@ void command_event_load_auto_state(void)
return;
#endif

strlcpy(savestate_name_auto,
_len = strlcpy(savestate_name_auto,
runloop_st->name.savestate,
sizeof(savestate_name_auto));
strlcat(savestate_name_auto,
strlcpy(savestate_name_auto + _len,
".auto",
sizeof(savestate_name_auto));
sizeof(savestate_name_auto) - _len);

if (!path_is_valid(savestate_name_auto))
return;
Expand Down
64 changes: 32 additions & 32 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -3648,21 +3648,20 @@ static bool config_load_file(global_t *global,
size_t _len = strlcpy(prefix, "input_player", sizeof(prefix));
for (i = 0; i < MAX_USERS; i++)
{
size_t _len2;
char buf[64];
buf[0] = '\0';
snprintf(prefix + _len, sizeof(prefix) - _len, "%u", i + 1);

strlcpy(buf, prefix, sizeof(buf));
strlcat(buf, "_analog_dpad_mode", sizeof(buf));
CONFIG_GET_INT_BASE(conf, settings, uints.input_analog_dpad_mode[i], buf);
_len2 = strlcpy(buf, prefix, sizeof(buf));

strlcpy(buf + _len2, "_mouse_index", sizeof(buf) - _len2);
CONFIG_GET_INT_BASE(conf, settings, uints.input_mouse_index[i], buf);

strlcpy(buf, prefix, sizeof(buf));
strlcat(buf, "_joypad_index", sizeof(buf));
strlcpy(buf + _len2, "_joypad_index", sizeof(buf) - _len2);
CONFIG_GET_INT_BASE(conf, settings, uints.input_joypad_index[i], buf);

strlcpy(buf, prefix, sizeof(buf));
strlcat(buf, "_mouse_index", sizeof(buf));
CONFIG_GET_INT_BASE(conf, settings, uints.input_mouse_index[i], buf);
strlcpy(buf + _len2, "_analog_dpad_mode", sizeof(buf) - _len2);
CONFIG_GET_INT_BASE(conf, settings, uints.input_analog_dpad_mode[i], buf);
}
}

Expand Down Expand Up @@ -4559,30 +4558,31 @@ static void video_driver_save_settings(global_t *global, config_file_t *conf)
static void save_keybind_hat(config_file_t *conf, const char *key,
const struct retro_keybind *bind)
{
size_t _len;
char config[16];
unsigned hat = (unsigned)GET_HAT(bind->joykey);

config[0] = 'h';
config[1] = '\0';

snprintf(config + 1, sizeof(config) - 1, "%u", hat);
_len = snprintf(config + 1, sizeof(config) - 1, "%u", hat);

switch (GET_HAT_DIR(bind->joykey))
{
case HAT_UP_MASK:
strlcat(config, "up", sizeof(config));
strlcpy(config + _len, "up", sizeof(config) - _len);
break;

case HAT_DOWN_MASK:
strlcat(config, "down", sizeof(config));
strlcpy(config + _len, "down", sizeof(config) - _len);
break;

case HAT_LEFT_MASK:
strlcat(config, "left", sizeof(config));
strlcpy(config, "left", sizeof(config) - _len);
break;

case HAT_RIGHT_MASK:
strlcat(config, "right", sizeof(config));
strlcpy(config, "right", sizeof(config) - _len);
break;

default:
Expand Down Expand Up @@ -5430,8 +5430,8 @@ int8_t config_save_overrides(enum override_type type, void *data, bool remove)
if (settings->uints.input_device[i]
!= overrides->uints.input_device[i])
{
strlcpy(cfg, "input_device_p", sizeof(cfg));
strlcat(cfg, formatted_number, sizeof(cfg));
size_t _len = strlcpy(cfg, "input_device_p", sizeof(cfg));
strlcpy(cfg + _len, formatted_number, sizeof(cfg) - _len);
config_set_int(conf, cfg, overrides->uints.input_device[i]);
RARCH_DBG("[Overrides]: %s = \"%u\"\n", cfg, overrides->uints.input_device[i]);
}
Expand Down Expand Up @@ -5652,8 +5652,8 @@ bool input_remapping_load_file(void *data, const char *path)
char formatted_number[4];
formatted_number[0] = '\0';
snprintf(formatted_number, sizeof(formatted_number), "%u", i + 1);
strlcpy(prefix, "input_player", sizeof(prefix));
strlcat(prefix, formatted_number, sizeof(prefix));
_len = strlcpy(prefix, "input_player", sizeof(prefix));
strlcpy(prefix + _len, formatted_number, sizeof(prefix) - _len);
_len = strlcpy(s1, prefix, sizeof(s1));
s1[_len ] = '_';
s1[_len+1] = 'b';
Expand Down Expand Up @@ -5734,16 +5734,16 @@ bool input_remapping_load_file(void *data, const char *path)
}
}

strlcpy(s1, prefix, sizeof(s1));
strlcat(s1, "_analog_dpad_mode", sizeof(s1));
_len = strlcpy(s1, prefix, sizeof(s1));
strlcpy(s1 + _len, "_analog_dpad_mode", sizeof(s1) - _len);
CONFIG_GET_INT_BASE(conf, settings, uints.input_analog_dpad_mode[i], s1);

strlcpy(s1, "input_libretro_device_p", sizeof(s1));
strlcat(s1, formatted_number, sizeof(s1));
_len = strlcpy(s1, "input_libretro_device_p", sizeof(s1));
strlcpy(s1 + _len, formatted_number, sizeof(s1) - _len);
CONFIG_GET_INT_BASE(conf, settings, uints.input_libretro_device[i], s1);

strlcpy(s1, "input_remap_port_p", sizeof(s1));
strlcat(s1, formatted_number, sizeof(s1));
_len = strlcpy(s1, "input_remap_port_p", sizeof(s1));
strlcpy(s1 + _len, formatted_number, sizeof(s1) - _len);
CONFIG_GET_INT_BASE(conf, settings, uints.input_remap_ports[i], s1);
}

Expand Down Expand Up @@ -5830,8 +5830,8 @@ bool input_remapping_save_file(const char *path)
continue;

snprintf(formatted_number, sizeof(formatted_number), "%u", i + 1);
strlcpy(prefix, "input_player", sizeof(prefix));
strlcat(prefix, formatted_number, sizeof(prefix));
_len = strlcpy(prefix, "input_player", sizeof(prefix));
strlcpy(prefix + _len, formatted_number, sizeof(prefix) - _len);
_len = strlcpy(s1, prefix, sizeof(s1));
s1[_len ] = '_';
s1[_len+1] = 'b';
Expand Down Expand Up @@ -5917,16 +5917,16 @@ bool input_remapping_save_file(const char *path)
settings->uints.input_keymapper_ids[i][j]);
}

strlcpy(s1, "input_libretro_device_p", sizeof(s1));
strlcat(s1, formatted_number, sizeof(s1));
_len = strlcpy(s1, "input_libretro_device_p", sizeof(s1));
strlcpy(s1 + _len, formatted_number, sizeof(s1) - _len);
config_set_int(conf, s1, input_config_get_device(i));

strlcpy(s1, prefix, sizeof(s1));
strlcat(s1, "_analog_dpad_mode", sizeof(s1));
_len = strlcpy(s1, prefix, sizeof(s1));
strlcpy(s1 + _len, "_analog_dpad_mode", sizeof(s1) - _len);
config_set_int(conf, s1, settings->uints.input_analog_dpad_mode[i]);

strlcpy(s1, "input_remap_port_p", sizeof(s1));
strlcat(s1, formatted_number, sizeof(s1));
_len = strlcpy(s1, "input_remap_port_p", sizeof(s1));
strlcpy(s1 + _len, formatted_number, sizeof(s1) - _len);
config_set_int(conf, s1, settings->uints.input_remap_ports[i]);
}

Expand Down
36 changes: 18 additions & 18 deletions core_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ static void core_info_path_list_free(core_path_list_t *path_list)
static core_path_list_t *core_info_path_list_new(const char *core_dir,
const char *core_exts, bool show_hidden_files)
{
size_t i;
size_t i, _len;
char exts[32];
core_path_list_t *path_list = NULL;
struct string_list *core_ext_list = NULL;
Expand Down Expand Up @@ -1375,11 +1375,12 @@ static core_path_list_t *core_info_path_list_new(const char *core_dir,

/* Get list of file extensions to include
* > core + lock */
strlcpy(exts, core_exts, sizeof(exts));
strlcat(exts, "|lck", sizeof(exts));
_len = strlcpy(exts, core_exts, sizeof(exts));
#if defined(HAVE_DYNAMIC)
/* > 'standalone exempt' */
strlcat(exts, "|lsae", sizeof(exts));
strlcpy(exts + _len, "|lck|lsae", sizeof(exts) - _len);
#else
strlcpy(exts + _len, "|lck", sizeof(exts) - _len);
#endif

/* Fetch core directory listing */
Expand Down Expand Up @@ -1617,38 +1618,37 @@ static void core_info_resolve_firmware(

for (i = 0; i < firmware_count; i++)
{
char path_key[64];
char desc_key[64];
char opt_key[64];
size_t _len2;
char key[64];
struct config_entry_list *entry = NULL;
bool tmp_bool = false;

snprintf(prefix + _len, sizeof(prefix) - _len, "%u_", i);
strlcpy(path_key, prefix, sizeof(path_key));
strlcat(path_key, "path", sizeof(path_key));
strlcpy(desc_key, prefix, sizeof(desc_key));
strlcat(desc_key, "desc", sizeof(desc_key));
strlcpy(opt_key, prefix, sizeof(opt_key));
strlcat(opt_key, "opt", sizeof(opt_key));
_len2 = strlcpy(key, prefix, sizeof(key));
strlcpy(key + _len2, "opt", sizeof(key) - _len2);

if (config_get_bool(conf, key, &tmp_bool))
firmware[i].optional = tmp_bool;

entry = config_get_entry(conf, path_key);
strlcpy(key + _len2, "path", sizeof(key) - _len2);

entry = config_get_entry(conf, key);

if (entry && !string_is_empty(entry->value))
{
firmware[i].path = entry->value;
entry->value = NULL;
}

entry = config_get_entry(conf, desc_key);
strlcpy(key + _len2, "desc", sizeof(key) - _len2);

entry = config_get_entry(conf, key);

if (entry && !string_is_empty(entry->value))
{
firmware[i].desc = entry->value;
entry->value = NULL;
}

if (config_get_bool(conf, opt_key , &tmp_bool))
firmware[i].optional = tmp_bool;
}

info->firmware_count = firmware_count;
Expand Down
4 changes: 2 additions & 2 deletions frontend/drivers/platform_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ static void frontend_win32_get_os(char *s, size_t len, int *major, int *minor)
case 2:
if (server)
{
strlcpy(s, "Windows Server 2003", len);
size_t _len = strlcpy(s, "Windows Server 2003", len);
if (GetSystemMetrics(SM_SERVERR2))
strlcat(s, " R2", len);
strlcpy(s + _len, " R2", len - _len);
}
else
{
Expand Down
16 changes: 7 additions & 9 deletions gfx/drivers/d3d10.c
Original file line number Diff line number Diff line change
Expand Up @@ -1351,24 +1351,22 @@ static bool d3d10_gfx_set_shader(void* data,
{ "TEXCOORD", 1, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(d3d10_vertex_t, texcoord),
D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
char vs_path[PATH_MAX_LENGTH];
char ps_path[PATH_MAX_LENGTH];
char _path[PATH_MAX_LENGTH];
const char *slang_path = d3d10->shader_preset->pass[i].source.path;
const char *vs_src = d3d10->shader_preset->pass[i].source.string.vertex;
const char *ps_src = d3d10->shader_preset->pass[i].source.string.fragment;

strlcpy(vs_path, slang_path, sizeof(vs_path));
strlcpy(ps_path, slang_path, sizeof(ps_path));
strlcat(vs_path, ".vs.hlsl", sizeof(vs_path));
strlcat(ps_path, ".ps.hlsl", sizeof(ps_path));
size_t _len = strlcpy(_path, slang_path, sizeof(_path));
strlcpy(_path + _len, ".vs.hlsl", sizeof(_path) - _len);

if (!d3d10_init_shader(
d3d10->device, vs_src, 0, vs_path, "main",
d3d10->device, vs_src, 0, _path, "main",
NULL, NULL, desc, countof(desc),
&d3d10->pass[i].shader)) { }

strlcpy(_path + _len, ".ps.hlsl", sizeof(_path) - _len);

if (!d3d10_init_shader(
d3d10->device, ps_src, 0, ps_path, NULL, "main",
d3d10->device, ps_src, 0, _path, NULL, "main",
NULL, NULL, 0,
&d3d10->pass[i].shader)) { }

Expand Down
Loading

0 comments on commit 631301b

Please sign in to comment.