]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
Q2BSP: fix misaligned memory access
authorbones_was_here <bones_was_here@xonotic.au>
Fri, 26 Jan 2024 10:10:14 +0000 (20:10 +1000)
committerbones_was_here <bones_was_here@xonotic.au>
Mon, 29 Jan 2024 15:32:38 +0000 (01:32 +1000)
Fixes a small overallocation (sizeof(int *) instead of int).

Signed-off-by: bones_was_here <bones_was_here@xonotic.au>
model_brush.c

index 98fc11fc8b5a26d07e53ccbc7009844e63b52969..092682b0de9dd5d673315bb9ce497e598b1d191a 100644 (file)
@@ -4824,7 +4824,8 @@ static void Mod_Q2BSP_Load(model_t *mod, void *buffer, void *bufferend)
        msurface_t *surface;
        int totalstylesurfaces, totalstyles, stylecounts[256], remapstyles[256];
        model_brush_lightstyleinfo_t styleinfo[256];
-       unsigned char *datapointer;
+       int *datapointer;
+       model_brush_lightstyleinfo_t *lsidatapointer;
        sizebuf_t sb;
 
        MSG_InitReadBuffer(&sb, (unsigned char *)buffer, (unsigned char *)bufferend - (unsigned char *)buffer);
@@ -4966,8 +4967,11 @@ static void Mod_Q2BSP_Load(model_t *mod, void *buffer, void *bufferend)
                                totalstylesurfaces += stylecounts[k];
                }
        }
-       datapointer = (unsigned char *)Mem_Alloc(mod->mempool, mod->num_surfaces * sizeof(int) + totalstyles * sizeof(model_brush_lightstyleinfo_t) + totalstylesurfaces * sizeof(int *));
-       mod->modelsurfaces_sorted = (int*)datapointer; datapointer += mod->num_surfaces * sizeof(int);
+       // bones_was_here: using a separate allocation for model_brush_lightstyleinfo_t
+       // because on a 64-bit machine it no longer has the same alignment requirement as int.
+       lsidatapointer = Mem_AllocType(mod->mempool, model_brush_lightstyleinfo_t, totalstyles * sizeof(model_brush_lightstyleinfo_t));
+       datapointer = Mem_AllocType(mod->mempool, int, mod->num_surfaces * sizeof(int) + totalstylesurfaces * sizeof(int));
+       mod->modelsurfaces_sorted = datapointer; datapointer += mod->num_surfaces;
        // set up the world model, then on each submodel copy from the world model
        // and set up the submodel with the respective model info.
        mod = loadmodel;
@@ -5070,7 +5074,7 @@ static void Mod_Q2BSP_Load(model_t *mod, void *buffer, void *bufferend)
                                        styleinfo[mod->brushq1.num_lightstyles].style = k;
                                        styleinfo[mod->brushq1.num_lightstyles].value = 0;
                                        styleinfo[mod->brushq1.num_lightstyles].numsurfaces = 0;
-                                       styleinfo[mod->brushq1.num_lightstyles].surfacelist = (int *)datapointer;datapointer += stylecounts[k] * sizeof(int);
+                                       styleinfo[mod->brushq1.num_lightstyles].surfacelist = datapointer;datapointer += stylecounts[k];
                                        remapstyles[k] = mod->brushq1.num_lightstyles;
                                        mod->brushq1.num_lightstyles++;
                                }
@@ -5087,7 +5091,7 @@ static void Mod_Q2BSP_Load(model_t *mod, void *buffer, void *bufferend)
                                        }
                                }
                        }
-                       mod->brushq1.data_lightstyleinfo = (model_brush_lightstyleinfo_t *)datapointer;datapointer += mod->brushq1.num_lightstyles * sizeof(model_brush_lightstyleinfo_t);
+                       mod->brushq1.data_lightstyleinfo = lsidatapointer;lsidatapointer += mod->brushq1.num_lightstyles;
                        memcpy(mod->brushq1.data_lightstyleinfo, styleinfo, mod->brushq1.num_lightstyles * sizeof(model_brush_lightstyleinfo_t));
                }
                else