Cleanup warnings: snprintf truncation, strncpy->memcpy, memset

This commit is contained in:
Zachary Cook 2020-05-17 18:17:32 -04:00
parent 1044eb4dce
commit 6b1d8f7144
7 changed files with 45 additions and 19 deletions

View File

@ -199,7 +199,13 @@ static bool firmware_is_present(unsigned region)
if (!bios_name_list[i])
break;
snprintf(bios_path, sizeof(bios_path), "%s%c%s", retro_base_directory, retro_slash, bios_name_list[i]);
int r = snprintf(bios_path, sizeof(bios_path), "%s%c%s", retro_base_directory, retro_slash, bios_name_list[i]);
if (r >= 4096)
{
log_cb(RETRO_LOG_ERROR, "Firmware path longer than 4095: %s\n", bios_path);
break;
}
if (filestream_exists(bios_path))
{
found = true;
@ -250,7 +256,7 @@ static void extract_basename(char *buf, const char *path, size_t size)
if (*base == '\\' || *base == '/')
base++;
strncpy(buf, base, size - 1);
strncpy(buf, base, size - strlen(buf) - 1);
buf[size - 1] = '\0';
char *ext = strrchr(buf, '.');
@ -4099,9 +4105,9 @@ bool retro_load_game(const struct retro_game_info *info)
extract_basename(retro_cd_base_name, info->path, sizeof(retro_cd_base_name));
extract_directory(retro_cd_base_directory, info->path, sizeof(retro_cd_base_directory));
snprintf(tocbasepath, sizeof(tocbasepath), "%s%c%s.toc", retro_cd_base_directory, retro_slash, retro_cd_base_name);
int r = snprintf(tocbasepath, sizeof(tocbasepath), "%s%c%s.toc", retro_cd_base_directory, retro_slash, retro_cd_base_name);
if (filestream_exists(tocbasepath))
if (r >= 0 && r < 4096 && filestream_exists(tocbasepath))
snprintf(retro_cd_path, sizeof(retro_cd_path), "%s", tocbasepath);
else
snprintf(retro_cd_path, sizeof(retro_cd_path), "%s", info->path);
@ -4993,25 +4999,32 @@ static void sanitize_path(std::string &path)
const char *MDFN_MakeFName(MakeFName_Type type, int id1, const char *cd1)
{
static char fullpath[4096];
int r = 0;
fullpath[0] = '\0';
switch (type)
{
case MDFNMKF_SAV:
snprintf(fullpath, sizeof(fullpath), "%s%c%s.%s",
r = snprintf(fullpath, sizeof(fullpath), "%s%c%s.%s",
retro_save_directory,
retro_slash,
shared_memorycards ? "mednafen_psx_libretro_shared" : retro_cd_base_name,
cd1);
break;
case MDFNMKF_FIRMWARE:
snprintf(fullpath, sizeof(fullpath), "%s%c%s", retro_base_directory, retro_slash, cd1);
r = snprintf(fullpath, sizeof(fullpath), "%s%c%s", retro_base_directory, retro_slash, cd1);
break;
default:
break;
}
if (r > 4095)
{
log_cb(RETRO_LOG_ERROR,"MakeFName path longer than 4095\n");
fullpath[4095] = '\0';
}
return fullpath;
}

View File

@ -745,7 +745,7 @@ void CDAccess_PBP::Eject(bool eject_status)
int CDAccess_PBP::decrypt_pgd(unsigned char* pgd_data, int pgd_size)
{
int result;
PGD_HEADER PGD[sizeof(PGD_HEADER)];
PGD_HEADER PGD[1];
MAC_KEY mkey;
CIPHER_KEY ckey;

View File

@ -185,9 +185,7 @@ void MDFNMP_RemoveReadPatches(void)
/* This function doesn't allocate any memory for "name" */
static int AddCheatEntry(char *name, char *conditions, uint32 addr, uint64 val, uint64 compare, int status, char type, unsigned int length, bool bigendian)
{
CHEATF temp;
memset(&temp, 0, sizeof(CHEATF));
CHEATF temp = CHEATF();
temp.name=name;
temp.conditions = conditions;
@ -597,7 +595,7 @@ int MDFNI_DecodePAR(const char *str, uint32 *a, uint8 *v, uint8 *c, char *type)
int boo[4];
if(strlen(str)!=8) return(0);
sscanf(str,"%02p%02p%02p%02p",boo,boo+1,boo+2,boo+3);
sscanf(str,"%02x%02x%02x%02x",boo,boo+1,boo+2,boo+3);
*c = 0;

View File

@ -119,7 +119,7 @@ void PS_CDC::SetDisc(bool tray_open, CDIF *cdif, const char *disc_id)
if(disc_id)
{
strncpy((char *)DiscID, disc_id, 4);
memcpy((char *)DiscID, disc_id, 4);
IsPSXDisc = true;
}
}

View File

@ -128,14 +128,26 @@ std::string MDFN_GetSettingS(const char *name)
if (!strcmp("filesys.fname_state", name))
{
char fullpath[4096];
snprintf(fullpath, sizeof(fullpath), "%s.sav", retro_cd_base_name);
return std::string(fullpath);
int r = snprintf(fullpath, sizeof(fullpath), "%s.sav", retro_cd_base_name);
if (r > 4095)
{
fprintf(stderr,"Path to .sav too long");
return 0;
}
else
return std::string(fullpath);
}
if (!strcmp("filesys.fname_sav", name))
{
char fullpath[4096];
snprintf(fullpath, sizeof(fullpath), "%s.bsv", retro_cd_base_name);
return std::string(fullpath);
int r = snprintf(fullpath, sizeof(fullpath), "%s.bsv", retro_cd_base_name);
if (r > 4095)
{
fprintf(stderr,"Path to .bsv too long");
return 0;
}
else
return std::string(fullpath);
}
fprintf(stderr, "unhandled setting S: %s\n", name);
return 0;

View File

@ -159,7 +159,10 @@ static bool SubWrite(StateMem *st, SFORMAT *sf, const char *name_prefix = NULL)
if (slen >= 255)
{
printf("Warning: state variable name possibly too long: %s %s %s %d\n", sf->name, name_prefix, nameo, slen);
if(name_prefix != NULL)
printf("Warning: state variable name possibly too long: %s %s %s %d\n", sf->name, name_prefix, nameo, slen);
else
printf("Warning: state variable name possibly too long: %s %s %d\n", sf->name, nameo, slen);
slen = 255;
}
@ -226,7 +229,7 @@ static int WriteStateChunk(StateMem *st, const char *sname, SFORMAT *sf)
uint8_t sname_tmp[32];
memset(sname_tmp, 0, sizeof(sname_tmp));
strncpy((char *)sname_tmp, sname, 32);
memcpy((char *)sname_tmp, sname, 32);
if(strlen(sname) > 32)
printf("Warning: section name is too long: %s\n", sname);

View File

@ -42,7 +42,7 @@ MDFN_PixelFormat::MDFN_PixelFormat(const unsigned int p_colorspace, const uint8
MDFN_Surface::MDFN_Surface()
{
memset(&format, 0, sizeof(format));
format = MDFN_PixelFormat();
pixels = NULL;
pitchinpix = 0;