R_GenerateBlendTables: allocate copy of palette for use in thread

The palette can be freed while this thread is still
running, which means the thread would end up reading from
freed memory.
This commit is contained in:
James R authored and Alug committed 2025-09-17 21:06:21 +02:00
1 parent f819471c06
commit 4dce42e458
2 files changed
+32 -11

No files matched your search

+31 -11
View File
@@ -111,14 +111,23 @@ static UINT8 **localtranslationtablecache[MAXLOCALSKINS] = {NULL};
CV_PossibleValue_t Color_cons_t[MAXSKINCOLORS+1];
static void R_GenerateBlendTables(void);
struct GenerateBlendTables_State
{
RGBA_t *LocalPalette;
};
static void R_GenerateBlendTables_Core(struct GenerateBlendTables_State *state);
static void R_AllocateBlendTables(void);
#ifdef HAVE_THREADS
static void R_GenerateBlendTables_Thread(void *userdata)
{
(void)userdata;
R_GenerateBlendTables();
struct GenerateBlendTables_State *state = static_cast<struct GenerateBlendTables_State *>(userdata);
R_GenerateBlendTables_Core(state);
free(state->LocalPalette);
free(state);
}
#endif
@@ -142,13 +151,7 @@ void R_InitTranslucencyTables(void)
W_ReadLump(W_GetNumForName("TRANS90"), transtables+0x80000);
R_AllocateBlendTables();
#ifdef HAVE_THREADS
I_spawn_thread("blend-tables",
R_GenerateBlendTables_Thread, NULL);
#else
R_GenerateBlendTables();
#endif
}
static colorlookup_t transtab_lut;
@@ -268,9 +271,26 @@ static void R_AllocateBlendTables(void)
}
}
static void R_GenerateBlendTables(void)
void R_GenerateBlendTables(void)
{
InitColorLUT(&transtab_lut, pLocalPalette, false);
#ifdef HAVE_THREADS
// Allocate copies for the worker thread since the originals can be freed in the main thread.
struct GenerateBlendTables_State *state = static_cast<struct GenerateBlendTables_State *>(malloc(sizeof *state));
size_t palsize = 256 * sizeof(RGBA_t);
state->LocalPalette = static_cast<RGBA_t *>(memcpy(malloc(palsize), pLocalPalette, palsize));
I_spawn_thread("blend-tables",
R_GenerateBlendTables_Thread, state);
#else
struct GenerateBlendTables_State state = {pLocalPalette, pGammaCorrectedPalette};
R_GenerateBlendTables_Core(&state);
#endif
}
static void R_GenerateBlendTables_Core(struct GenerateBlendTables_State *state)
{
InitColorLUT(&transtab_lut, state->LocalPalette, false);
// Additive
BlendTab_GenerateMaps(blendtab_add, AST_ADD, BlendTab_Translucent);
+1
View File
@@ -212,6 +212,7 @@ enum
extern UINT8 *blendtables[NUMBLENDMAPS];
void R_InitTranslucencyTables(void);
void R_GenerateBlendTables(void);
UINT8 *R_GetTranslucencyTable(INT32 alphalevel);
UINT8 *R_GetBlendTable(int style, INT32 alphalevel);