From a6a4b845a4eb9f281a40c24812faf0172a33f982 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Mon, 25 Jul 2022 04:39:08 +0200 Subject: [PATCH] Some string simplifications - don't NULL terminate if we pass string to strlcpy and/or a file_path function using strlcpy under the hood - don't do strlcpy for trivial setting of 2/3 char strings --- gfx/common/win32_common.c | 11 +++------ gfx/common/x11_common.c | 26 ++++++++------------ input/input_driver.c | 6 ++--- libretro-common/file/archive_file.c | 38 +++++++++-------------------- libretro-common/net/net_http.c | 4 +-- menu/menu_explore.c | 5 +++- 6 files changed, 34 insertions(+), 56 deletions(-) diff --git a/gfx/common/win32_common.c b/gfx/common/win32_common.c index 0bab51155d..939ea5dbde 100644 --- a/gfx/common/win32_common.c +++ b/gfx/common/win32_common.c @@ -635,14 +635,15 @@ static bool win32_browser( char new_title[PATH_MAX]; char new_file[32768]; - new_title[0] = '\0'; - new_file[0] = '\0'; - if (!string_is_empty(title)) strlcpy(new_title, title, sizeof(new_title)); + else + new_title[0] = '\0'; if (filename && *filename) strlcpy(new_file, filename, sizeof(new_file)); + else + new_file[0] = '\0'; /* OPENFILENAME.lpstrFilters is actually const, * so this cast should be safe */ @@ -2345,12 +2346,8 @@ void win32_update_title(void) if (window) { char title[128]; - - title[0] = '\0'; - video_driver_get_window_title(title, sizeof(title)); update_title_wait = g_win32_refresh_rate; - if (title[0]) window->set_title(&main_window, title); } diff --git a/gfx/common/x11_common.c b/gfx/common/x11_common.c index 99fff79ed7..81f80e2940 100644 --- a/gfx/common/x11_common.c +++ b/gfx/common/x11_common.c @@ -203,31 +203,32 @@ static void xdg_screensaver_inhibit(Window wnd) { int ret; char cmd[64]; - char title[128]; cmd[0] = '\0'; - title[0] = '\0'; RARCH_LOG("[X11]: Suspending screensaver (X11, xdg-screensaver).\n"); if (g_x11_dpy && g_x11_win) { + char title[128]; + size_t title_len; /* Make sure the window has a title, even if it's a bogus one, otherwise * xdg-screensaver will fail and report to stderr, framing RA for its bug. * A single space character is used so that the title bar stays visibly * the same, as if there's no title at all. */ video_driver_get_window_title(title, sizeof(title)); - if (strlen(title) == 0) - snprintf(title, sizeof(title), " "); + if ((title_len = strlen(title)) == 0) + { + title[0] = ' '; + title[1] = '\0'; + } XChangeProperty(g_x11_dpy, g_x11_win, XA_WM_NAME, XA_STRING, - 8, PropModeReplace, (const unsigned char*) title, - strlen(title)); + 8, PropModeReplace, (const unsigned char*) title, title_len); } snprintf(cmd, sizeof(cmd), "xdg-screensaver suspend 0x%x", (int)wnd); - ret = system(cmd); - if (ret == -1) + if ((ret = system(cmd)) == -1) { xdg_screensaver_available = false; RARCH_WARN("Failed to launch xdg-screensaver.\n"); @@ -519,18 +520,15 @@ static void x11_handle_key_event(unsigned keycode, XEvent *event, XIC ic, bool f keybuf[0] = '\0'; #ifdef X_HAVE_UTF8_STRING status = 0; - /* XwcLookupString doesn't seem to work. */ num = Xutf8LookupString(ic, &event->xkey, keybuf, ARRAY_SIZE(keybuf), &keysym, &status); - /* libc functions need UTF-8 locale to work properly, * which makes mbrtowc a bit impractical. * * Use custom UTF8 -> UTF-32 conversion. */ num = utf8_conv_utf32(chars, ARRAY_SIZE(chars), keybuf, num); #else - (void)ic; num = XLookupString(&event->xkey, keybuf, sizeof(keybuf), &keysym, NULL); /* ASCII only. */ for (i = 0; i < num; i++) @@ -584,7 +582,7 @@ bool x11_alive(void *data) /* IMPORTANT - Get keycode before XFilterEvent because the event is localizated after the call */ keycode = event.xkey.keycode; - filter = XFilterEvent(&event, g_x11_win); + filter = XFilterEvent(&event, g_x11_win); switch (event.type) { @@ -769,11 +767,7 @@ bool x11_connect(void) void x11_update_title(void *data) { char title[128]; - - title[0] = '\0'; - video_driver_get_window_title(title, sizeof(title)); - if (title[0]) XChangeProperty(g_x11_dpy, g_x11_win, XA_WM_NAME, XA_STRING, 8, PropModeReplace, (const unsigned char*)title, diff --git a/input/input_driver.c b/input/input_driver.c index faced91b55..32f200668f 100644 --- a/input/input_driver.c +++ b/input/input_driver.c @@ -3080,8 +3080,6 @@ void config_read_keybinds_conf(void *data) if (!btn || !prefix) continue; - str[0] = '\0'; - fill_pathname_join_delim(str, prefix, btn, '_', sizeof(str)); /* Clear old mapping bit */ @@ -5675,8 +5673,8 @@ void input_keyboard_event(bool down, unsigned code, if (say_char) { - char c = (char) character; - *say_char = c; + char c = (char) character; + *say_char = c; say_char[1] = '\0'; if (character == 127 || character == 8) diff --git a/libretro-common/file/archive_file.c b/libretro-common/file/archive_file.c index 660f2d56d0..0ece100aae 100644 --- a/libretro-common/file/archive_file.c +++ b/libretro-common/file/archive_file.c @@ -104,15 +104,13 @@ static int file_archive_extract_cb(const char *name, const char *valid_exts, char new_path[PATH_MAX_LENGTH]; const char *delim; - delim = path_get_archive_delim(userdata->archive_path); - - if (delim) + if ((delim = path_get_archive_delim(userdata->archive_path))) { - if (!string_is_equal_noncase(userdata->current_file_path, delim + 1)) + if (!string_is_equal_noncase( + userdata->current_file_path, delim + 1)) return 1; /* keep searching for the right file */ } - new_path[0] = '\0'; if (userdata->extraction_directory) fill_pathname_join(new_path, userdata->extraction_directory, path_basename(name), sizeof(new_path)); @@ -120,7 +118,6 @@ static int file_archive_extract_cb(const char *name, const char *valid_exts, fill_pathname_resolve_relative(new_path, userdata->archive_path, path_basename(name), sizeof(new_path)); - if (file_archive_perform_mode(new_path, valid_exts, cdata, cmode, csize, size, checksum, userdata)) @@ -141,25 +138,18 @@ static int file_archive_parse_file_init(file_archive_transfer_t *state, char path[PATH_MAX_LENGTH]; char *last = NULL; - path[0] = '\0'; - strlcpy(path, file, sizeof(path)); - last = (char*)path_get_archive_delim(path); + if ((last = (char*)path_get_archive_delim(path))) + *last = '\0'; - if (last) - *last = '\0'; - - state->backend = file_archive_get_file_backend(path); - if (!state->backend) + if (!(state->backend = file_archive_get_file_backend(path))) return -1; - state->archive_file = filestream_open(path, - RETRO_VFS_FILE_ACCESS_READ, - RETRO_VFS_FILE_ACCESS_HINT_NONE); - /* Failed to open archive. */ - if (!state->archive_file) + if (!(state->archive_file = filestream_open(path, + RETRO_VFS_FILE_ACCESS_READ, + RETRO_VFS_FILE_ACCESS_HINT_NONE))) return -1; state->archive_size = filestream_get_size(state->archive_file); @@ -648,16 +638,12 @@ const struct file_archive_file_backend* file_archive_get_file_backend(const char const char *file_ext = NULL; char *last = NULL; - newpath[0] = '\0'; - strlcpy(newpath, path, sizeof(newpath)); - last = (char*)path_get_archive_delim(newpath); + if ((last = (char*)path_get_archive_delim(newpath))) + *last = '\0'; - if (last) - *last = '\0'; - - file_ext = path_get_extension(newpath); + file_ext = path_get_extension(newpath); #ifdef HAVE_7ZIP if (string_is_equal_noncase(file_ext, "7z")) diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index fc9be82dc3..503feac98b 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -381,10 +381,10 @@ void net_http_urlencode(char **dest, const char *source) void net_http_urlencode_full(char *dest, const char *source, size_t size) { + char url_domain[256]; + char url_path[PATH_MAX_LENGTH]; size_t buf_pos = 0; char *tmp = NULL; - char url_domain[256] = {0}; - char url_path[PATH_MAX_LENGTH] = {0}; int count = 0; strlcpy(url_path, source, sizeof(url_path)); diff --git a/menu/menu_explore.c b/menu/menu_explore.c index fb8fa6fff2..310c04d378 100644 --- a/menu/menu_explore.c +++ b/menu/menu_explore.c @@ -807,7 +807,10 @@ static int explore_action_sublabel_spacer( * unnecessarily blank out the fallback * core title text in the sublabel area */ if (string_is_equal(menu_driver, "ozone")) - strlcpy(s, " ", len); + { + s[0] = ' '; + s[1] = '\0'; + } return 1; /* 1 means it'll never change and can be cached */ }