Skip to content

Commit

Permalink
Avoid some strlens, and also return size_t in more string
Browse files Browse the repository at this point in the history
modification functions
  • Loading branch information
LibretroAdmin committed Jan 12, 2025
1 parent 598764b commit 7e72241
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 85 deletions.
86 changes: 46 additions & 40 deletions cheat_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ unsigned cheat_manager_get_size(void)
#ifdef HAVE_CHEEVOS
static void cheat_manager_pause_cheevos(void)
{
const char *msg = msg_hash_to_str(MSG_CHEEVOS_HARDCORE_MODE_DISABLED_CHEAT);
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CHEEVOS_HARDCORE_MODE_DISABLED_CHEAT), sizeof(msg));
rcheevos_pause_hardcore();

runloop_msg_queue_push(msg, strlen(msg), 1, 180, true, NULL,
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_LOG("%s\n", msg);
}
Expand Down Expand Up @@ -106,10 +106,11 @@ void cheat_manager_apply_cheats(void)

if (cheat_st->size > 0 && settings->bools.notification_show_cheats_applied)
{
const char *_msg = msg_hash_to_str(MSG_APPLYING_CHEAT);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_APPLYING_CHEAT), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_LOG("%s\n", _msg);
RARCH_LOG("%s\n", msg);
}

#ifdef HAVE_CHEEVOS
Expand Down Expand Up @@ -854,8 +855,9 @@ int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool w
meminfo.id = RETRO_MEMORY_SYSTEM_RAM;
if (!core_get_memory(&meminfo))
{
const char *_msg = msg_hash_to_str(MSG_CHEAT_INIT_FAIL);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_INIT_FAIL), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return 0;
}
Expand Down Expand Up @@ -885,7 +887,6 @@ int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool w

if (is_search_initialization)
{
const char *msg = NULL;
if (cheat_st->prev_memory_buf)
{
free(cheat_st->prev_memory_buf);
Expand All @@ -897,8 +898,9 @@ int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool w

if (!cheat_st->prev_memory_buf)
{
const char *_msg = msg_hash_to_str(MSG_CHEAT_INIT_FAIL);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_INIT_FAIL), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return 0;
}
Expand All @@ -914,10 +916,11 @@ int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool w

if (!cheat_st->matches)
{
const char *_msg = msg_hash_to_str(MSG_CHEAT_INIT_FAIL);
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_INIT_FAIL), sizeof(msg));
free(cheat_st->prev_memory_buf);
cheat_st->prev_memory_buf = NULL;
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return 0;
}
Expand All @@ -934,9 +937,12 @@ int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool w
offset += cheat_st->memory_size_list[i];
}

msg = msg_hash_to_str(MSG_CHEAT_INIT_SUCCESS);
runloop_msg_queue_push(msg, strlen(msg), 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
{
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_INIT_SUCCESS), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}

cheat_st->memory_search_initialized = true;
}
Expand Down Expand Up @@ -1034,8 +1040,8 @@ static int cheat_manager_search(enum cheat_search_type search_type)

if (cheat_st->num_memory_buffers == 0 || !prev || !cheat_st->matches)
{
const char *msg = msg_hash_to_str(MSG_CHEAT_SEARCH_NOT_INITIALIZED);
runloop_msg_queue_push(msg, strlen(msg), 1, 180, true, NULL,
_len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_SEARCH_NOT_INITIALIZED), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return 0;
}
Expand Down Expand Up @@ -1241,8 +1247,8 @@ int cheat_manager_add_matches(const char *path,

if (cheat_st->num_matches + cheat_st->size > 100)
{
const char *_msg = msg_hash_to_str(MSG_CHEAT_SEARCH_ADDED_MATCHES_TOO_MANY);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
_len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_SEARCH_ADDED_MATCHES_TOO_MANY), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return 0;
}
Expand Down Expand Up @@ -1281,8 +1287,8 @@ int cheat_manager_add_matches(const char *path,
if (!cheat_manager_add_new_code(cheat_st->search_bit_size, idx, (mask << (byte_part * bits)),
cheat_st->big_endian, curr_val))
{
const char *_msg = msg_hash_to_str(MSG_CHEAT_SEARCH_ADDED_MATCHES_FAIL);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
_len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_SEARCH_ADDED_MATCHES_FAIL), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return 0;
}
Expand All @@ -1296,8 +1302,8 @@ int cheat_manager_add_matches(const char *path,
if (!cheat_manager_add_new_code(cheat_st->search_bit_size, idx, 0xFF,
cheat_st->big_endian, curr_val))
{
const char *_msg = msg_hash_to_str(MSG_CHEAT_SEARCH_ADDED_MATCHES_FAIL);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 180, true, NULL,
_len = strlcpy(msg, msg_hash_to_str(MSG_CHEAT_SEARCH_ADDED_MATCHES_FAIL), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return 0;
}
Expand Down Expand Up @@ -1638,30 +1644,30 @@ void cheat_manager_match_action(enum cheat_match_action_type match_action, unsig

switch (bytes_per_item)
{
case 2:
curr_val = cheat_st->big_endian ?
case 2:
curr_val = cheat_st->big_endian ?
(*(curr + idx - offset) * 256) + *(curr + idx + 1 - offset) :
*(curr + idx - offset) + (*(curr + idx + 1 - offset) * 256);
if (prev)
prev_val = cheat_st->big_endian ?
if (prev)
prev_val = cheat_st->big_endian ?
(*(prev + idx) * 256) + *(prev + idx + 1) :
*(prev + idx) + (*(prev + idx + 1) * 256);
break;
case 4:
curr_val = cheat_st->big_endian ?
break;
case 4:
curr_val = cheat_st->big_endian ?
(*(curr + idx - offset) * 256 * 256 * 256) + (*(curr + idx + 1 - offset) * 256 * 256) + (*(curr + idx + 2 - offset) * 256) + *(curr + idx + 3 - offset) :
*(curr + idx - offset) + (*(curr + idx + 1 - offset) * 256) + (*(curr + idx + 2 - offset) * 256 * 256) + (*(curr + idx + 3 - offset) * 256 * 256 * 256);
if (prev)
prev_val = cheat_st->big_endian ?
if (prev)
prev_val = cheat_st->big_endian ?
(*(prev + idx) * 256 * 256 * 256) + (*(prev + idx + 1) * 256 * 256) + (*(prev + idx + 2) * 256) + *(prev + idx + 3) :
*(prev + idx) + (*(prev + idx + 1) * 256) + (*(prev + idx + 2) * 256 * 256) + (*(prev + idx + 3) * 256 * 256 * 256);
break;
case 1:
default:
curr_val = *(curr + idx - offset);
if (prev)
prev_val = *(prev + idx);
break;
break;
case 1:
default:
curr_val = *(curr + idx - offset);
if (prev)
prev_val = *(prev + idx);
break;
}

if (match_action == CHEAT_MATCH_ACTION_TYPE_BROWSE)
Expand Down
15 changes: 9 additions & 6 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -4469,8 +4469,9 @@ bool config_load_override(void *data)
if (settings->bools.notification_show_config_override_load
&& show_notification)
{
const char *_msg = msg_hash_to_str(MSG_CONFIG_OVERRIDE_LOADED);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 100, false, NULL,
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CONFIG_OVERRIDE_LOADED), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 100, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}

Expand Down Expand Up @@ -4525,8 +4526,9 @@ bool config_load_override_file(const char *config_path)
if (settings->bools.notification_show_config_override_load
&& show_notification)
{
const char *_msg = msg_hash_to_str(MSG_CONFIG_OVERRIDE_LOADED);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 100, false, NULL,
char msg[128];
size_t _len = strlcpy(msg, msg_hash_to_str(MSG_CONFIG_OVERRIDE_LOADED), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 100, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}

Expand Down Expand Up @@ -4735,8 +4737,9 @@ bool config_load_remap(const char *directory_input_remapping,
success:
if (notification_show_remap_load)
{
const char *_msg = msg_hash_to_str(msg_remap_loaded);
runloop_msg_queue_push(_msg, strlen(_msg), 1, 100, false, NULL,
char _msg[128];
size_t _len = strlcpy(_msg, msg_hash_to_str(msg_remap_loaded), sizeof(_msg));
runloop_msg_queue_push(_msg, _len, 1, 100, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
return true;
Expand Down
76 changes: 39 additions & 37 deletions disk_control_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,58 +273,55 @@ void disk_control_get_image_label(
* Generates an appropriate log/notification message
* for a disk index change event
**/
static void disk_control_get_index_set_msg(
static size_t disk_control_get_index_set_msg(
disk_control_interface_t *disk_control,
unsigned num_images, unsigned index, bool success,
unsigned *msg_duration, char *msg, size_t len)
unsigned *msg_duration, char *s, size_t len)
{
bool has_label = false;
char image_label[128];

image_label[0] = '\0';

if (!disk_control || !msg_duration || !msg || len < 1)
return;

/* Attempt to get image label */
size_t _len = 0;
if (!disk_control || !msg_duration || !s || len < 1)
return 0;
/* Check whether image was inserted or removed */
if (index < num_images)
{
char image_label[128];
bool has_label = false;
image_label[0] = '\0';
disk_control_get_image_label(
disk_control, index, image_label, sizeof(image_label));
has_label = !string_is_empty(image_label);
}

/* Get message duration
* > Default is 60
* > If a label is shown, then increase duration by 50%
* > For errors, duration is always 180 */
*msg_duration = success ?
(has_label ? 90 : 60) :
180;
has_label = !string_is_empty(image_label);

/* Check whether image was inserted or removed */
if (index < num_images)
{
size_t _len = strlcpy(msg,
/* Get message duration
* > Default is 60
* > If a label is shown, then increase duration by 50%
* > For errors, duration is always 180 */
*msg_duration = success ? (has_label ? 90 : 60) : 180;

_len = strlcpy(s,
success
? msg_hash_to_str(MSG_SETTING_DISK_IN_TRAY)
: msg_hash_to_str(MSG_FAILED_TO_SET_DISK), len);
if (has_label)
snprintf(
msg + _len, len - _len, ": %u/%u - %s",
_len += snprintf(
s + _len, len - _len, ": %u/%u - %s",
index + 1, num_images, image_label);
else
snprintf(
msg + _len, len - _len, ": %u/%u",
_len += snprintf(
s + _len, len - _len, ": %u/%u",
index + 1, num_images);
}
else
strlcpy(
msg,
{
*msg_duration = success ? 60 : 180;
_len += strlcpy(
s,
success
? msg_hash_to_str(MSG_REMOVED_DISK_FROM_TRAY)
: msg_hash_to_str(MSG_FAILED_TO_REMOVE_DISK_FROM_TRAY),
len);
}
return _len;
}

/**
Expand Down Expand Up @@ -404,6 +401,7 @@ bool disk_control_set_index(
disk_control_interface_t *disk_control,
unsigned index, bool verbosity)
{
size_t _len;
bool error = false;
unsigned num_images = 0;
unsigned msg_duration = 0;
Expand All @@ -430,12 +428,12 @@ bool disk_control_set_index(
error = !disk_control->cb.set_image_index(index);

/* Get log/notification message */
disk_control_get_index_set_msg(
_len = disk_control_get_index_set_msg(
disk_control, num_images, index, !error,
&msg_duration, msg, sizeof(msg));

/* Output log/notification message */
if (!string_is_empty(msg))
if (_len > 0)
{
if (error)
RARCH_ERR("[Disc]: %s\n", msg);
Expand All @@ -444,7 +442,7 @@ bool disk_control_set_index(

/* Errors should always be displayed */
if (verbosity || error)
runloop_msg_queue_push(msg, strlen(msg), 1, msg_duration, true, NULL,
runloop_msg_queue_push(msg, _len, 1, msg_duration, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}

Expand Down Expand Up @@ -799,7 +797,10 @@ bool disk_control_verify_initial_index(
/* If current disk is incorrect, notify user */
if (!success && enabled)
{
const char *_msg = msg_hash_to_str(MSG_FAILED_TO_SET_INITIAL_DISK);
char _msg[128];
size_t _len = strlcpy(_msg,
msg_hash_to_str(MSG_FAILED_TO_SET_INITIAL_DISK), sizeof(_msg));

RARCH_ERR(
"[Disc]: Failed to set initial disc index:\n> Expected"
" [%u] %s\n> Detected [%u] %s\n",
Expand All @@ -810,7 +811,7 @@ bool disk_control_verify_initial_index(

/* Ignore 'verbosity' setting - errors should
* always be displayed */
runloop_msg_queue_push(_msg, strlen(_msg), 0, 60, true, NULL,
runloop_msg_queue_push(_msg, _len, 0, 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);

/* Since a failure here typically means that the
Expand All @@ -835,12 +836,13 @@ bool disk_control_verify_initial_index(
* is available */
if (disk_control->initial_num_images > 1)
{
size_t _len;
unsigned msg_duration = 0;
char msg[128];

msg[0] = '\0';

disk_control_get_index_set_msg(
_len = disk_control_get_index_set_msg(
disk_control, disk_control->initial_num_images, image_index, true,
&msg_duration, msg, sizeof(msg));

Expand All @@ -852,7 +854,7 @@ bool disk_control_verify_initial_index(
* we do not want to 'overwrite' them */
if (verbosity)
runloop_msg_queue_push(
msg, strlen(msg), 0, msg_duration, false, NULL,
msg, _len, 0, msg_duration, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);

#ifdef HAVE_CHEEVOS
Expand Down
5 changes: 3 additions & 2 deletions gfx/gfx_widgets.c
Original file line number Diff line number Diff line change
Expand Up @@ -1123,12 +1123,13 @@ static int gfx_widgets_draw_indicator(
}
else
{
char txt[NAME_MAX_LENGTH];
unsigned height = p_dispwidget->simple_widget_height;
const char *txt = msg_hash_to_str(msg);
size_t _len = strlcpy(txt, msg_hash_to_str(msg), sizeof(txt));

width = font_driver_get_message_width(
p_dispwidget->gfx_widget_fonts.regular.font,
txt, strlen(txt), 1.0f)
txt, _len, 1.0f)
+ p_dispwidget->simple_widget_padding * 2;

gfx_display_draw_quad(
Expand Down

0 comments on commit 7e72241

Please sign in to comment.