diff --git a/Quake/fs_zip.c b/Quake/fs_zip.c index 36e7ae7e..0b4f1ff6 100644 --- a/Quake/fs_zip.c +++ b/Quake/fs_zip.c @@ -766,6 +766,7 @@ FILE *FSZIP_Deflate(FILE *src, qofs_t srcsize, qofs_t outsize) if (!of) { + Con_Printf("FSZIP_Deflate: tmpfile failed, out of handles?\n"); fclose(src); return NULL; } @@ -800,16 +801,21 @@ FILE *FSZIP_Deflate(FILE *src, qofs_t srcsize, qofs_t outsize) inflateEnd(&strm); fclose(src); fclose(of); - Con_Printf("Couldn't decompress file\n"); + Con_Printf("Couldn't decompress file, corrupt?\n"); return NULL; } - } fwrite(outbuffer, 1, strm.total_out, of); inflateEnd(&strm); - fclose(src); + if (strm.total_out != outsize) + { + Con_Printf("Decompressed %u bytes, expected %u\n", (unsigned int)strm.total_out, (unsigned int)outsize); + fclose(of); + return NULL; + } + fseek(of, SEEK_SET, 0); return of; #else diff --git a/Quake/gl_model.c b/Quake/gl_model.c index 29ade8cd..9857ec73 100644 --- a/Quake/gl_model.c +++ b/Quake/gl_model.c @@ -3120,10 +3120,11 @@ static void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype) pheader->gltextures[i][0] = TexMgr_LoadImage (loadmodel, filename, fwidth, fheight, fmt, data, filename, 0, TEXPREF_ALPHA|texflags|TEXPREF_MIPMAP ); - //now try to load glow/luma image from the same place if (malloced) free(data); Hunk_FreeToLowMark (mark); + + //now try to load glow/luma image from the same place q_snprintf (filename2, sizeof(filename2), "%s_glow", filename); data = !gl_load24bit.value?NULL:Image_LoadImage (filename2, &fwidth, &fheight, &fmt, &malloced); if (!data) @@ -3137,6 +3138,10 @@ static void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype) fmt, data, filename, 0, TEXPREF_ALPHA|texflags|TEXPREF_MIPMAP ); else pheader->fbtextures[i][0] = NULL; + + if (malloced) + free(data); + Hunk_FreeToLowMark (mark); } else { diff --git a/Quake/image.c b/Quake/image.c index f6758b1e..0a9e1121 100644 --- a/Quake/image.c +++ b/Quake/image.c @@ -106,13 +106,17 @@ byte *Image_LoadPNG(FILE *f, int *width, int *height, qboolean *malloced) in = malloc(com_filesize); if (!in) + { + fclose(f); return NULL; + } if (com_filesize == fread(in, 1, com_filesize, f)) { *malloced = true; lodepng_decode32(&out, &w, &h, in, insize); } free(in); + fclose(f); return out; #endif } @@ -175,11 +179,11 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt fread(&fmtheader, 1, sizeof(fmtheader), f); if (fmtheader.magic != (('D'<<0)|('D'<<8)|('S'<<16)|(' '<<24))) - return NULL; + goto fail; fmtheader.dwSize += sizeof(fmtheader.magic); if (fmtheader.dwSize != sizeof(fmtheader)) - return NULL; //corrupt/different version + goto fail; //corrupt/different version memset(&fmt10header, 0, sizeof(fmt10header)); fmt10header.arraysize = (fmtheader.ddsCaps[1] & 0x200)?6:1; //cubemaps need 6 faces... @@ -228,7 +232,7 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt Con_Printf(" blue: %08x\n", fmtheader.ddpfPixelFormat.bluemask); Con_Printf("alpha: %08x\n", fmtheader.ddpfPixelFormat.alphamask); Con_Printf(" used: %08x\n", fmtheader.ddpfPixelFormat.redmask^fmtheader.ddpfPixelFormat.greenmask^fmtheader.ddpfPixelFormat.bluemask^fmtheader.ddpfPixelFormat.alphamask); - return NULL; + goto fail; } #undef IsPacked } @@ -426,7 +430,7 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt default: Con_Printf("Unsupported dds10 dxgi in %s - %u\n", fname, fmt10header.dxgiformat); - return NULL; + goto fail; } } if (encoding == SRC_EXTERNAL) //used as an error code @@ -436,40 +440,40 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt ((char*)&fmtheader.ddpfPixelFormat.dwFourCC)[1], ((char*)&fmtheader.ddpfPixelFormat.dwFourCC)[2], ((char*)&fmtheader.ddpfPixelFormat.dwFourCC)[3]); - return NULL; + goto fail; } if ((fmtheader.ddsCaps[1] & 0x200) && (fmtheader.ddsCaps[1] & 0xfc00) != 0xfc00) - return NULL; //cubemap without all 6 faces defined. + goto fail; //cubemap without all 6 faces defined. if (fmtheader.ddsCaps[1] & 0x200) { if (fmt10header.arraysize % 6) //weird number of faces. - return NULL; + goto fail; if (fmt10header.arraysize == 6) { //layers = 6; - return NULL; //don't try to load cubemaps. + goto fail; //don't try to load cubemaps. } else { //layers = fmt10header.arraysize; - return NULL; //don't try to load cubemap arrays. + goto fail; //don't try to load cubemap arrays. } } else if (fmtheader.ddsCaps[1] & 0x200000) { if (fmt10header.arraysize != 1) //no 2d arrays - return NULL; - return NULL; //don't try to load 3d textures. + goto fail; + goto fail; //don't try to load 3d textures. } else { if (fmt10header.arraysize == 1) ; //yay, we can load 2d images. else - return NULL; //don't try to load 2d arrays. + goto fail; //don't try to load 2d arrays. //layers = fmt10header.arraysize; } @@ -485,22 +489,28 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt if (mipnum != nummips) { Con_Printf("%s: dds with incomplete mip chain\n", fname); - return NULL; + goto fail; } datasize = TexMgr_ImageSize(fmtheader.dwWidth, fmtheader.dwHeight, encoding); if (!datasize) - return NULL; //werid/unsupported + goto fail; //werid/unsupported //just read the mipchain into a new bit of memory and return that. //note that layers and mips are awkward, but we don't support layers here so its just a densely packed pyramid. ret = (byte *) Hunk_Alloc (datasize); fread(ret, 1, datasize, f); + fclose(f); + *width = fmtheader.dwWidth; *height = fmtheader.dwHeight; *fmt = encoding; return ret; + +fail: + fclose(f); + return NULL; } /*spike -- end of dds loader*/