* Prevent multiple strlen calls if one will do

* Use strlcpy instead of snprintf if we just need to copy a string
* Use fill_pathname_join where appropriate
This commit is contained in:
libretroadmin 2022-07-22 15:14:48 +02:00
parent dd6192e94e
commit 39d612dbd2
4 changed files with 22 additions and 20 deletions

View File

@ -8,6 +8,8 @@
#include <string.h>
#include <ctype.h>
#include <compat/strl.h>
#include "rd_util.h"
#include "rd_output.h"
@ -91,10 +93,7 @@ static void write_u32(FILE *f, unsigned int n)
static void write_ch8(FILE *f, const char *s)
{
char buffer[9];
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s", s);
strlcpy(buffer, s, sizeof(buffer));
fwrite(buffer, 8, 1, f);
}

View File

@ -8,6 +8,9 @@
#include <string.h>
#include <stdarg.h>
#include <compat/strl.h>
#include <file/file_path.h>
#include "rd_util.h"
void ATTR((noreturn)) die(const char *error, ...)
@ -65,19 +68,18 @@ size_t read_or_die(void **ptr, const char *file)
void *buffer = NULL, *pos = buffer;
FILE *f;
f = fopen(file, "rb");
if (!f)
if (!(f = fopen(file, "rb")))
{
int i;
size_t s;
char *path;
char *path = NULL;
size_t file_len = strlen(file);
for (i = 0; i < num_search_paths; i++)
{
s = strlen(search_paths[i]) + 1 + strlen(file) + 1;
path = xmalloc(s);
snprintf(path, s, "%s/%s", search_paths[i], file);
f = fopen(path, "rb");
size_t s = strlen(search_paths[i]) + 1 + file_len + 1;
path = xmalloc(s);
fill_pathname_join(path, search_paths[i], file, s);
f = fopen(path, "rb");
free(path);
}
}

View File

@ -11,6 +11,7 @@
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <array/rbuf.h>
#include <compat/strl.h>
#if _MSC_VER
#include <compat/msvc.h>
@ -532,7 +533,7 @@ static void update_variables(bool startup)
{
char *pch;
char str[100];
snprintf(str, sizeof(str), "%s", var.value);
strlcpy(str, var.value, sizeof(str));
pch = strtok(str, "x");
if (pch)
@ -821,7 +822,7 @@ bool retro_load_game(const struct retro_game_info *info)
if (!use_external_savedir)
{
// > Use WAD directory fallback...
snprintf(g_save_dir, sizeof(g_save_dir), "%s", g_wad_dir);
strlcpy(g_save_dir, g_wad_dir, sizeof(g_save_dir));
}
}
@ -855,8 +856,7 @@ failed:
struct retro_message msg;
char msg_local[256];
snprintf(msg_local, sizeof(msg_local),
"ROM loading failed...");
strlcpy(msg_local, "ROM loading failed...", sizeof(msg_local));
msg.msg = msg_local;
msg.frames = 360;
if (environ_cb)

View File

@ -101,9 +101,10 @@ void D_CheckNetGame(void)
dbool D_NetGetWad(const char* name)
{
#if defined(HAVE_WAIT_H)
size_t psize = sizeof(packet_header_t) + strlen(name) + 500;
packet_header_t *packet;
dbool done = FALSE;
size_t name_len = strlen(name);
size_t psize = sizeof(packet_header_t) + name_len + 500;
dbool done = FALSE;
if (!server || strchr(name, '/')) return FALSE; // If it contains path info, reject
@ -113,7 +114,7 @@ dbool D_NetGetWad(const char* name)
packet_set(packet, PKT_WAD, 0);
*(uint8_t*)(packet+1) = consoleplayer;
strcpy(1+(uint8_t*)(packet+1), name);
I_SendPacket(packet, sizeof(packet_header_t) + strlen(name) + 2);
I_SendPacket(packet, sizeof(packet_header_t) + name_len + 2);
I_uSleep(10000);
} while (!I_GetPacket(packet, psize) || (packet->type != PKT_WAD));
@ -122,7 +123,7 @@ dbool D_NetGetWad(const char* name)
if (!strcasecmp((void*)(packet+1), name)) {
pid_t pid;
int rv;
uint8_t *p = (uint8_t*)(packet+1) + strlen(name) + 1;
uint8_t *p = (uint8_t*)(packet+1) + name_len + 1;
/* Automatic wad file retrieval using wget (supports http and ftp, using URLs)
* Unix systems have all these commands handy, this kind of thing is easy