From 9692484d2e1a75a5a52b9e1e332e2c8a82b2fabc Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 20 Jul 2026 11:29:19 +0200 Subject: [PATCH] fix issues with sound freeslotting this "works" with a skinlimit of 255 but would cause hard to debug crashes with raised skinlimits and was generally really unsafe and could cause soundnames to not be null terminated aswell as buffer overflows from blind strcpy the sprintf fix is from Indev originally for neptune NOTE: if the skinlimit is ever raised above 1024 then freeslotnames NEEDS to be atleast one byte bigger to not truncuate stuff NOTE2: pretty sure localskins are fucked if any are loaded in servers with 255 skins loaded, but i dont care atm --- src/sounds.c | 23 ++++++++++++----------- src/sounds.h | 2 ++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/sounds.c b/src/sounds.c index 3769bf65b..e17e79512 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -972,7 +972,7 @@ sfxinfo_t S_sfx[NUMSFX] = // initialized to NULL }; -char freeslotnames[sfx_freeslot0 + NUMSFXFREESLOTS + NUMSKINSFXSLOTS][7] = {}; +static char freeslotnames[sfx_freeslot0 + NUMSFXFREESLOTS + NUMSKINSFXSLOTS][MAXSOUNDNAME] = {}; // Prepare free sfx slots to add sfx at run time void S_InitRuntimeSounds(void) @@ -985,16 +985,13 @@ void S_InitRuntimeSounds(void) { value = (i+1) - sfx_freeslot0; - if (value < 10) - sprintf(soundname, "fre00%d", value); - else if (value < 100) - sprintf(soundname, "fre0%d", value); - else if (value < 1000) - sprintf(soundname, "fre%d", value); - else - sprintf(soundname, "fr%d", value); + sprintf(soundname, "fr%04x", value); - strcpy(freeslotnames[value-1], soundname); + // this might happen when raising skinlimit + I_Assert(strlen(soundname) < MAXSOUNDNAME); + + strncpy(freeslotnames[value-1], soundname, MAXSOUNDNAME-1); + freeslotnames[value-1][MAXSOUNDNAME-1] = '\0'; S_sfx[i].name = freeslotnames[value-1]; S_sfx[i].singularity = false; @@ -1024,7 +1021,11 @@ sfxenum_t S_AddSoundFx(const char *name, boolean singular, INT32 flags, boolean { if (!S_sfx[i].priority) { - strncpy(freeslotnames[i-sfx_freeslot0], name, 6); + I_Assert(strlen(name) < MAXSOUNDNAME); + + strncpy(freeslotnames[i - sfx_freeslot0], name, MAXSOUNDNAME-1); + freeslotnames[i - sfx_freeslot0][MAXSOUNDNAME-1] = '\0'; + S_sfx[i].singularity = singular; S_sfx[i].priority = 60; S_sfx[i].pitch = flags; diff --git a/src/sounds.h b/src/sounds.h index ea530da05..a8522f1c6 100644 --- a/src/sounds.h +++ b/src/sounds.h @@ -59,6 +59,8 @@ typedef enum #define NUMSFXFREESLOTS 1600 // Matches SOC Editor. #define NUMSKINSFXSLOTS (MAXSKINS*NUMSKINSOUNDS) +#define MAXSOUNDNAME 7 + // // SoundFX struct. //