From ca7e53e3ca5e0f9929ec69cd5c2335f8aa63b0b8 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Mon, 23 Dec 2024 00:40:09 +0100 Subject: [PATCH] string_replace_substring - move out implicit strlen --- gfx/video_shader_parse.c | 114 +++++++++--------- gfx/video_shader_parse.h | 2 +- .../formats/logiqx_dat/logiqx_dat.c | 10 +- libretro-common/include/string/stdstring.h | 5 +- libretro-common/string/stdstring.c | 7 +- msg_hash.c | 4 +- 6 files changed, 72 insertions(+), 70 deletions(-) diff --git a/gfx/video_shader_parse.c b/gfx/video_shader_parse.c index 4341be081f..b5af272d3f 100644 --- a/gfx/video_shader_parse.c +++ b/gfx/video_shader_parse.c @@ -63,21 +63,21 @@ enum wildcard_type { - RARCH_WILDCARD_CONTENT_DIR = 0, - RARCH_WILDCARD_CORE, - RARCH_WILDCARD_GAME, - RARCH_WILDCARD_VIDEO_DRIVER, - RARCH_WILDCARD_VIDEO_USER_ROTATION, - RARCH_WILDCARD_VIDEO_ALLOW_CORE_ROTATION, - RARCH_WILDCARD_CORE_REQUESTED_ROTATION, - RARCH_WILDCARD_VIDEO_FINAL_ROTATION, - RARCH_WILDCARD_SCREEN_ORIENTATION, - RARCH_WILDCARD_VIEWPORT_ASPECT_ORIENTATION, - RARCH_WILDCARD_CORE_ASPECT_ORIENTATION, - RARCH_WILDCARD_PRESET_DIR, - RARCH_WILDCARD_PRESET, - RARCH_WILDCARD_VIDEO_DRIVER_SHADER_EXT, - RARCH_WILDCARD_VIDEO_DRIVER_PRESET_EXT + RARCH_WC_CONTENT_DIR = 0, + RARCH_WC_CORE, + RARCH_WC_GAME, + RARCH_WC_VIDEO_DRIVER, + RARCH_WC_VIDEO_USER_ROTATION, + RARCH_WC_VIDEO_ALLOW_CORE_ROTATION, + RARCH_WC_CORE_REQUESTED_ROTATION, + RARCH_WC_VIDEO_FINAL_ROTATION, + RARCH_WC_SCREEN_ORIENTATION, + RARCH_WC_VIEWPORT_ASPECT_ORIENTATION, + RARCH_WC_CORE_ASPECT_ORIENTATION, + RARCH_WC_PRESET_DIR, + RARCH_WC_PRESET, + RARCH_WC_VIDEO_DRIVER_SHADER_EXT, + RARCH_WC_VIDEO_DRIVER_PRESET_EXT }; struct wildcard_token @@ -87,24 +87,6 @@ struct wildcard_token size_t token_size; }; -static struct wildcard_token wildcard_tokens[SHADER_NUM_WILDCARDS] = { - {RARCH_WILDCARD_CONTENT_DIR, "$CONTENT-DIR$", STRLEN_CONST("$CONTENT-DIR$")}, - {RARCH_WILDCARD_CORE, "$CORE$", STRLEN_CONST("$CORE$")}, - {RARCH_WILDCARD_GAME, "$GAME$", STRLEN_CONST("$GAME$")}, - {RARCH_WILDCARD_VIDEO_DRIVER, "$VID-DRV$", STRLEN_CONST("$VID-DRV$")}, - {RARCH_WILDCARD_VIDEO_DRIVER_PRESET_EXT, "$VID-DRV-PRESET-EXT$", STRLEN_CONST("$VID-DRV-PRESET-EXT$")}, - {RARCH_WILDCARD_VIDEO_DRIVER_SHADER_EXT, "$VID-DRV-SHADER-EXT$", STRLEN_CONST("$VID-DRV-SHADER-EXT$")}, - {RARCH_WILDCARD_CORE_REQUESTED_ROTATION, "$CORE-REQ-ROT$", STRLEN_CONST("$CORE-REQ-ROT$")}, - {RARCH_WILDCARD_VIDEO_ALLOW_CORE_ROTATION, "$VID-ALLOW-CORE-ROT$", STRLEN_CONST("$VID-ALLOW-CORE-ROT$")}, - {RARCH_WILDCARD_VIDEO_USER_ROTATION, "$VID-USER-ROT$", STRLEN_CONST("$VID-USER-ROT$")}, - {RARCH_WILDCARD_VIDEO_FINAL_ROTATION, "$VID-FINAL-ROT$", STRLEN_CONST("$VID-FINAL-ROT$")}, - {RARCH_WILDCARD_SCREEN_ORIENTATION, "$SCREEN-ORIENT$", STRLEN_CONST("$SCREEN-ORIENT$")}, - {RARCH_WILDCARD_VIEWPORT_ASPECT_ORIENTATION, "$VIEW-ASPECT-ORIENT$", STRLEN_CONST("$VIEW-ASPECT-ORIENT$")}, - {RARCH_WILDCARD_CORE_ASPECT_ORIENTATION, "$CORE-ASPECT-ORIENT$", STRLEN_CONST("$CORE-ASPECT-ORIENT$")}, - {RARCH_WILDCARD_PRESET_DIR, "$PRESET-DIR$", STRLEN_CONST("$PRESET-DIR$")}, - {RARCH_WILDCARD_PRESET, "$PRESET$", STRLEN_CONST("$PRESET$")}, -}; - /* TODO/FIXME - global state - perhaps move outside this file */ static path_change_data_t *file_change_data = NULL; @@ -222,20 +204,39 @@ static void fill_pathname_expanded_and_absolute( **/ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_path) { - int i = 0; + static struct wildcard_token wildcard_tokens[SHADER_NUM_WILDCARDS] = { + {RARCH_WC_CONTENT_DIR, "$CONTENT-DIR$", STRLEN_CONST("$CONTENT-DIR$")}, + {RARCH_WC_CORE, "$CORE$", STRLEN_CONST("$CORE$")}, + {RARCH_WC_GAME, "$GAME$", STRLEN_CONST("$GAME$")}, + {RARCH_WC_VIDEO_DRIVER, "$VID-DRV$", STRLEN_CONST("$VID-DRV$")}, + {RARCH_WC_VIDEO_DRIVER_PRESET_EXT, "$VID-DRV-PRESET-EXT$", STRLEN_CONST("$VID-DRV-PRESET-EXT$")}, + {RARCH_WC_VIDEO_DRIVER_SHADER_EXT, "$VID-DRV-SHADER-EXT$", STRLEN_CONST("$VID-DRV-SHADER-EXT$")}, + {RARCH_WC_CORE_REQUESTED_ROTATION, "$CORE-REQ-ROT$", STRLEN_CONST("$CORE-REQ-ROT$")}, + {RARCH_WC_VIDEO_ALLOW_CORE_ROTATION, "$VID-ALLOW-CORE-ROT$", STRLEN_CONST("$VID-ALLOW-CORE-ROT$")}, + {RARCH_WC_VIDEO_USER_ROTATION, "$VID-USER-ROT$", STRLEN_CONST("$VID-USER-ROT$")}, + {RARCH_WC_VIDEO_FINAL_ROTATION, "$VID-FINAL-ROT$", STRLEN_CONST("$VID-FINAL-ROT$")}, + {RARCH_WC_SCREEN_ORIENTATION, "$SCREEN-ORIENT$", STRLEN_CONST("$SCREEN-ORIENT$")}, + {RARCH_WC_VIEWPORT_ASPECT_ORIENTATION, "$VIEW-ASPECT-ORIENT$", STRLEN_CONST("$VIEW-ASPECT-ORIENT$")}, + {RARCH_WC_CORE_ASPECT_ORIENTATION, "$CORE-ASPECT-ORIENT$", STRLEN_CONST("$CORE-ASPECT-ORIENT$")}, + {RARCH_WC_PRESET_DIR, "$PRESET-DIR$", STRLEN_CONST("$PRESET-DIR$")}, + {RARCH_WC_PRESET, "$PRESET$", STRLEN_CONST("$PRESET$")}, + }; + + int i; + size_t _len; settings_t *settings; char replaced_path[PATH_MAX_LENGTH]; - if (!strstr(s, RARCH_WILDCARD_DELIMITER)) + if (!strstr(s, RARCH_WC_DELIMITER)) return; settings = config_get_ptr(); - strlcpy(replaced_path, s, sizeof(replaced_path)); + _len = strlcpy(replaced_path, s, sizeof(replaced_path)); /* Step through the wildcards while we can still find the * delimiter in the replaced path */ - for (i = 0; (i < SHADER_NUM_WILDCARDS) && (strstr(replaced_path, RARCH_WILDCARD_DELIMITER)); i++) + for (i = 0; (i < SHADER_NUM_WILDCARDS) && (strstr(replaced_path, RARCH_WC_DELIMITER)); i++) { /* If the wildcard text is in the path then process it */ if (strstr(replaced_path, wildcard_tokens[i].token_name)) @@ -245,7 +246,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ switch (wildcard_tokens[i].token_id) { - case RARCH_WILDCARD_CONTENT_DIR: + case RARCH_WC_CONTENT_DIR: { char content_dir_name[DIR_MAX_LENGTH] = ""; const char *rarch_path_basename = path_get(RARCH_PATH_BASENAME); @@ -261,10 +262,10 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ replace_text[0] = '\0'; } break; - case RARCH_WILDCARD_CORE: + case RARCH_WC_CORE: replace_len = strlcpy(replace_text, runloop_state_get_ptr()->system.info.library_name, sizeof(replace_text)); break; - case RARCH_WILDCARD_GAME: + case RARCH_WC_GAME: { const char *rarch_path_basename = path_get(RARCH_PATH_BASENAME); if (rarch_path_basename) @@ -275,10 +276,10 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ replace_text[0] = '\0'; } break; - case RARCH_WILDCARD_VIDEO_DRIVER: + case RARCH_WC_VIDEO_DRIVER: replace_len = strlcpy(replace_text, settings->arrays.video_driver, sizeof(replace_text)); break; - case RARCH_WILDCARD_CORE_REQUESTED_ROTATION: + case RARCH_WC_CORE_REQUESTED_ROTATION: replace_len = strlcpy(replace_text, "CORE-REQ-ROT-", sizeof(replace_text)); replace_len += snprintf( @@ -287,7 +288,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ "%d", retroarch_get_core_requested_rotation() * 90); break; - case RARCH_WILDCARD_VIDEO_ALLOW_CORE_ROTATION: + case RARCH_WC_VIDEO_ALLOW_CORE_ROTATION: replace_len = strlcpy(replace_text, "VID-ALLOW-CORE-ROT-O", sizeof(replace_text)); if (settings->bools.video_allow_rotate) @@ -297,7 +298,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ replace_len += strlcpy(replace_text + replace_len, "FF", sizeof(replace_text) - replace_len); break; - case RARCH_WILDCARD_VIDEO_USER_ROTATION: + case RARCH_WC_VIDEO_USER_ROTATION: replace_len = strlcpy(replace_text, "VID-USER-ROT-", sizeof(replace_text)); replace_len += snprintf( @@ -306,7 +307,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ "%d", settings->uints.video_rotation * 90); break; - case RARCH_WILDCARD_VIDEO_FINAL_ROTATION: + case RARCH_WC_VIDEO_FINAL_ROTATION: replace_len = strlcpy(replace_text, "VID-FINAL-ROT-", sizeof(replace_text)); @@ -316,7 +317,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ "%d", settings->uints.video_rotation * 90); break; - case RARCH_WILDCARD_SCREEN_ORIENTATION: + case RARCH_WC_SCREEN_ORIENTATION: replace_len = strlcpy(replace_text, "SCREEN-ORIENT-", sizeof(replace_text)); @@ -326,7 +327,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ "%d", settings->uints.screen_orientation * 90); break; - case RARCH_WILDCARD_CORE_ASPECT_ORIENTATION: + case RARCH_WC_CORE_ASPECT_ORIENTATION: { int requested_rotation; replace_len = strlcpy(replace_text, @@ -339,7 +340,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ sizeof(replace_text) - replace_len); } break; - case RARCH_WILDCARD_VIEWPORT_ASPECT_ORIENTATION: + case RARCH_WC_VIEWPORT_ASPECT_ORIENTATION: { unsigned viewport_width = 0; unsigned viewport_height = 0; @@ -353,7 +354,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ sizeof(replace_text) - replace_len); } break; - case RARCH_WILDCARD_PRESET_DIR: + case RARCH_WC_PRESET_DIR: { char preset_dir_name[DIR_MAX_LENGTH]; fill_pathname_parent_dir_name(preset_dir_name, in_preset_path, sizeof(preset_dir_name)); @@ -365,7 +366,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ replace_text[0] = '\0'; } break; - case RARCH_WILDCARD_PRESET: + case RARCH_WC_PRESET: { char preset_name[NAME_MAX_LENGTH]; fill_pathname(preset_name, @@ -377,7 +378,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ replace_text[0] = '\0'; } break; - case RARCH_WILDCARD_VIDEO_DRIVER_SHADER_EXT: + case RARCH_WC_VIDEO_DRIVER_SHADER_EXT: { gfx_ctx_flags_t flags; flags.flags = 0; @@ -393,7 +394,7 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ replace_text[0] = '\0'; } break; - case RARCH_WILDCARD_VIDEO_DRIVER_PRESET_EXT: + case RARCH_WC_VIDEO_DRIVER_PRESET_EXT: { gfx_ctx_flags_t flags; flags.flags = 0; @@ -414,11 +415,10 @@ static void video_shader_replace_wildcards(char *s, size_t len, char *in_preset_ break; } { - char *replace_output = string_replace_substring(replaced_path, - wildcard_tokens[i].token_name, - wildcard_tokens[i].token_size, - replace_text, - replace_len); + char *replace_output = string_replace_substring( + replaced_path, _len, + wildcard_tokens[i].token_name, wildcard_tokens[i].token_size, + replace_text, replace_len); strlcpy(replaced_path, replace_output, sizeof(replaced_path)); diff --git a/gfx/video_shader_parse.h b/gfx/video_shader_parse.h index 002734c62e..ef7d411498 100644 --- a/gfx/video_shader_parse.h +++ b/gfx/video_shader_parse.h @@ -42,7 +42,7 @@ #define GFX_MAX_FRAME_HISTORY 128 #endif -#define RARCH_WILDCARD_DELIMITER "$" +#define RARCH_WC_DELIMITER "$" /** * video_shader_parse_type: diff --git a/libretro-common/formats/logiqx_dat/logiqx_dat.c b/libretro-common/formats/logiqx_dat/logiqx_dat.c index bd02c5240d..a81708925b 100644 --- a/libretro-common/formats/logiqx_dat/logiqx_dat.c +++ b/libretro-common/formats/logiqx_dat/logiqx_dat.c @@ -35,12 +35,12 @@ struct logiqx_dat /* List of HTML formatting codes that must * be replaced when parsing XML data */ -const char *logiqx_dat_html_code_list[][2] = { +const char *logiqx_dat_html_code_list[][2] = { {"&", "&"}, {"'", "'"}, - {">", ">"}, + {">", ">"}, {"<", "<"}, - {""", "\""} + {""", "\""} }; #define LOGIQX_DAT_HTML_CODE_LIST_SIZE 5 @@ -53,7 +53,7 @@ const char *logiqx_dat_html_code_list[][2] = { * Also provides access to file size (DAT files can * be very large, so it is useful to have this information * on hand - i.e. so we can check that the system has - * enough free memory to load the file). */ + * enough free memory to load the file). */ bool logiqx_dat_path_is_valid(const char *path, uint64_t *file_size) { const char *file_ext = NULL; @@ -254,7 +254,7 @@ static void logiqx_dat_sanitise_element_data( if (strstr(sanitised_data, find_string)) { char *tmp = string_replace_substring( - sanitised_data, + sanitised_data, strlen(sanitised_data), find_string, strlen(find_string), replace_string, strlen(replace_string)); diff --git a/libretro-common/include/string/stdstring.h b/libretro-common/include/string/stdstring.h index 030cc61202..e7cb76c712 100644 --- a/libretro-common/include/string/stdstring.h +++ b/libretro-common/include/string/stdstring.h @@ -159,8 +159,9 @@ char *string_to_lower(char *s); char *string_ucwords(char *s); -char *string_replace_substring(const char *in, - const char *pattern, size_t pattern_len, +char *string_replace_substring( + const char *in, size_t in_len, + const char *pattern, size_t pattern_len, const char *replacement, size_t replacement_len); /** diff --git a/libretro-common/string/stdstring.c b/libretro-common/string/stdstring.c index 7f508ad7e7..85468b91eb 100644 --- a/libretro-common/string/stdstring.c +++ b/libretro-common/string/stdstring.c @@ -77,8 +77,9 @@ char *string_ucwords(char *s) return s; } -char *string_replace_substring(const char *in, - const char *pattern, size_t pattern_len, +char *string_replace_substring( + const char *in, size_t in_len, + const char *pattern, size_t pattern_len, const char *replacement, size_t replacement_len) { size_t outlen; @@ -101,7 +102,7 @@ char *string_replace_substring(const char *in, numhits++; } - outlen = strlen(in) - pattern_len*numhits + replacement_len*numhits; + outlen = in_len - pattern_len * numhits + replacement_len*numhits; if (!(out = (char *)malloc(outlen+1))) return NULL; diff --git a/msg_hash.c b/msg_hash.c index a9d68d5709..79726bd7f2 100644 --- a/msg_hash.c +++ b/msg_hash.c @@ -42,7 +42,7 @@ int msg_hash_get_help_enum(enum msg_hash_enums msg, char *s, size_t len) { int ret = msg_hash_get_help_us_enum(msg, s, len); /* Replace line-breaks with "empty line-breaks" for readability */ - const char *temp = string_replace_substring(s, + const char *temp = string_replace_substring(s, strlen(s), "\n", STRLEN_CONST("\n"), "\n \n", STRLEN_CONST("\n \n")); @@ -972,6 +972,6 @@ const char *msg_hash_get_wideglyph_str(void) break; } #endif - + return NULL; }