From b2072dd3bea580bd04450a2726361fe361e4042a Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Wed, 15 Jan 2025 16:36:49 +0100 Subject: [PATCH] Use strldup where possible, avoid strcpy_literal --- libretro-common/media/media_detect_cd.c | 40 +++++++------------------ libretro-db/query.c | 19 +++++------- runahead.c | 18 ++--------- 3 files changed, 20 insertions(+), 57 deletions(-) diff --git a/libretro-common/media/media_detect_cd.c b/libretro-common/media/media_detect_cd.c index 68831c4b3209..dd0cf1fb8c15 100644 --- a/libretro-common/media/media_detect_cd.c +++ b/libretro-common/media/media_detect_cd.c @@ -328,7 +328,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ * but I have not seen any real examples of them. */ info->system_id = MEDIA_CD_SYSTEM_MEGA_CD; - strcpy_literal(info->system, "Sega CD / Mega CD"); + strlcpy(info->system, "Sega CD / Mega CD", sizeof(info->system)); title_pos = buf + offset + 0x150; @@ -338,12 +338,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ media_zero_trailing_spaces(info->title, sizeof(info->title)); } else - { - info->title[0] = 'N'; - info->title[1] = '/'; - info->title[2] = 'A'; - info->title[3] = '\0'; - } + strlcpy(info->title, "N/A", sizeof(info->title)); serial_pos = buf + offset + 0x183; @@ -370,7 +365,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ info->system_id = MEDIA_CD_SYSTEM_SATURN; - strcpy_literal(info->system, "Sega Saturn"); + strlcpy(info->system, "Sega Saturn", sizeof(info->system)); title_pos = buf + offset + 0x60; @@ -380,12 +375,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ media_zero_trailing_spaces(info->title, sizeof(info->title)); } else - { - info->title [0] = 'N'; - info->title [1] = '/'; - info->title [2] = 'A'; - info->title [3] = '\0'; - } + strlcpy(info->title, "N/A", sizeof(info->title)); serial_pos = buf + offset + 0x20; @@ -441,7 +431,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ info->system_id = MEDIA_CD_SYSTEM_DREAMCAST; - strcpy_literal(info->system, "Sega Dreamcast"); + strlcpy(info->system, "Sega Dreamcast", sizeof(info->system)); title_pos = buf + offset + 0x80; @@ -451,12 +441,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ media_zero_trailing_spaces(info->title, sizeof(info->title)); } else - { - info->title [0] = 'N'; - info->title [1] = '/'; - info->title [2] = 'A'; - info->title [3] = '\0'; - } + strlcpy(info->title, "N/A", sizeof(info->title)); serial_pos = buf + offset + 0x40; @@ -510,7 +495,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ info->system_id = MEDIA_CD_SYSTEM_PSX; - strcpy_literal(info->system, "Sony PlayStation"); + strlcpy(info->system, "Sony PlayStation", sizeof(info->system)); title_pos = buf + offset + (16 * sector_size) + 40; @@ -520,22 +505,17 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_ media_zero_trailing_spaces(info->title, sizeof(info->title)); } else - { - info->title [0] = 'N'; - info->title [1] = '/'; - info->title [2] = 'A'; - info->title [3] = '\0'; - } + strlcpy(info->title, "N/A", sizeof(info->title)); } else if (!memcmp(buf + offset, "\x01\x5a\x5a\x5a\x5a\x5a\x01\x00\x00\x00\x00\x00", 12)) { info->system_id = MEDIA_CD_SYSTEM_3DO; - strcpy_literal(info->system, "3DO"); + strlcpy(info->system, "3DO", sizeof(info->system)); } else if (!memcmp(buf + offset + 0x950, "PC Engine CD-ROM SYSTEM", 23)) { info->system_id = MEDIA_CD_SYSTEM_PC_ENGINE_CD; - strcpy_literal(info->system, "TurboGrafx-CD / PC-Engine CD"); + strlcpy(info->system, "TurboGrafx-CD / PC-Engine CD", sizeof(info->system)); } free(buf); diff --git a/libretro-db/query.c b/libretro-db/query.c index 370d2b665c87..7fcf86bdf13d 100644 --- a/libretro-db/query.c +++ b/libretro-db/query.c @@ -272,15 +272,13 @@ static void query_raise_unknown_function( ssize_t where, const char *name, ssize_t len, const char **error) { - int n = snprintf(s, _len, + int __len = snprintf(s, _len, "%" PRIu64 "::Unknown function '", (uint64_t)where ); - - if (len < ((ssize_t)_len - n - 3)) - strncpy(s + n, name, len); - - strcpy_literal(s + n + len, "'"); + if (len < ((ssize_t)_len - __len - 3)) + strncpy(s + __len, name, len); + strcpy_literal(s + __len + len, "'"); *error = s; } @@ -671,8 +669,7 @@ static struct buffer query_parse_method_call( { if (argi >= QUERY_MAX_ARGS) { - strcpy_literal(s, - "Too many arguments in function call."); + strlcpy(s, "Too many arguments in function call.", len); *error = s; goto clean; } @@ -797,8 +794,7 @@ static struct buffer query_parse_table( { if (argi >= QUERY_MAX_ARGS) { - strcpy_literal(s, - "Too many arguments in function call."); + strlcpy(s, "Too many arguments in function call.", len); *error = s; goto clean; } @@ -846,8 +842,7 @@ static struct buffer query_parse_table( if (argi >= QUERY_MAX_ARGS) { - strcpy_literal(s, - "Too many arguments in function call."); + strlcpy(s, "Too many arguments in function call.", len); *error = s; goto clean; } diff --git a/runahead.c b/runahead.c index 2714aaf69844..12147a31455e 100644 --- a/runahead.c +++ b/runahead.c @@ -150,11 +150,7 @@ static void strcat_alloc(char **dst, const char *s) { size_t __len = strlen(s); if (__len != 0) - { - char *_dst= (char*)malloc(__len + 1); - strcpy_literal(_dst, s); - src = _dst; - } + src = strldup(s, __len); else src = NULL; } @@ -243,11 +239,7 @@ static char *get_tmpdir_alloc(const char *override_dir) { size_t _len = strlen(src); if (_len != 0) - { - char *dst = (char*)malloc(_len + 1); - strcpy_literal(dst, src); - path = dst; - } + path = strldup(src, _len); } else path = (char*)calloc(1,1); @@ -272,11 +264,7 @@ static bool write_file_with_random_name(char **temp_dll_path, { size_t _len = strlen(src); if (_len != 0) - { - char *dst = (char*)malloc(_len + 1); - strcpy_literal(dst, src); - ext = dst; - } + ext = strldup(src, _len); } else ext = (char*)calloc(1,1);