* 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 <string.h>
#include <ctype.h> #include <ctype.h>
#include <compat/strl.h>
#include "rd_util.h" #include "rd_util.h"
#include "rd_output.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) static void write_ch8(FILE *f, const char *s)
{ {
char buffer[9]; char buffer[9];
strlcpy(buffer, s, sizeof(buffer));
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s", s);
fwrite(buffer, 8, 1, f); fwrite(buffer, 8, 1, f);
} }

View File

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

View File

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

View File

@ -101,9 +101,10 @@ void D_CheckNetGame(void)
dbool D_NetGetWad(const char* name) dbool D_NetGetWad(const char* name)
{ {
#if defined(HAVE_WAIT_H) #if defined(HAVE_WAIT_H)
size_t psize = sizeof(packet_header_t) + strlen(name) + 500;
packet_header_t *packet; 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 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); packet_set(packet, PKT_WAD, 0);
*(uint8_t*)(packet+1) = consoleplayer; *(uint8_t*)(packet+1) = consoleplayer;
strcpy(1+(uint8_t*)(packet+1), name); 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); I_uSleep(10000);
} while (!I_GetPacket(packet, psize) || (packet->type != PKT_WAD)); } 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)) { if (!strcasecmp((void*)(packet+1), name)) {
pid_t pid; pid_t pid;
int rv; 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) /* 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 * Unix systems have all these commands handy, this kind of thing is easy