Heap corruption when R_InitSkyMap changes height of sky texture
Created attachment 515 [details]
Test case: 512x128 sky texture made from two 256x200 patches
R_InitTextures reads and parses TEXTURE1/PNAMES, then calls R_GenerateLookup for each texture to precalculate an allocation size (texturecompositesize[texnum]). The allocation is not made (Z_Malloc2 is not called) until R_GenerateComposite is called to build the texture, on its first use in a level.
However, between these two steps, R_InitSkyMap is called, which detects that a sky of height H1 (128 in this case) is composed of patches of height H2 (200 in this case). Noticing H2 > H1, it sets the height of the texture to H2 without making the necessary adjustment to the precalculated allocation size.
Therefore the allocation made later in R_GenerateComposite is too small, and data is written past the end of the allocated block, resulting in heap corruption, which manifests as a segfault on the next call to Z_Malloc2.
As the wad with the original problem (dk_dm_4.wad) has been fixed, I attach a test case. 256x200.wad replicates the former situation in dk_dm_4.wad of a 512x128 sky texture composed from two 256x200 patches.
- Call R_GenerateLookup again in R_InitSkyMap if the latter decides to change the sky texture's height.
- Stop fiddling with the texture height in R_InitSkyMap. (Why is fiddling the height necessary at all? It seems like a hack to support some WAD with broken texture definitions...)
- Dispense with the preallocation step entirely, and do all texture composition lazily. Remove texturecompositesize, merge R_GenerateLookup into R_GenerateComposite.
Created attachment 516 [details]
Recalculate texture allocation if R_InitSkyMap changes its height
This fixes the crash in 256x200.wad (see other attachment).
But it seems like a hack. Making texture composition more lazy seems like a better solution.
Trippy wad, patch committed in 5144, will leave bug open for further review
Marking assigned as work has been done on this bug.
I don't see why we are fiddling with the height there. It's not in 1.23 code. I've removed it and this then fixes bug 1129.
Is this considered resolved?