Skip to content

Commit

Permalink
Use strldup where possible, avoid strcpy_literal
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jan 15, 2025
1 parent 9da7af4 commit b2072dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 57 deletions.
40 changes: 10 additions & 30 deletions libretro-common/media/media_detect_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down
19 changes: 7 additions & 12 deletions libretro-db/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 3 additions & 15 deletions runahead.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b2072dd

Please sign in to comment.