Try to diagnose/avoid whatever filehandle leakage there is.

This commit is contained in:
Shpoike 2022-09-11 14:24:48 +01:00
parent 4d57e5fccc
commit 52c0b4e364
3 changed files with 39 additions and 18 deletions

View file

@ -766,6 +766,7 @@ FILE *FSZIP_Deflate(FILE *src, qofs_t srcsize, qofs_t outsize)
if (!of) if (!of)
{ {
Con_Printf("FSZIP_Deflate: tmpfile failed, out of handles?\n");
fclose(src); fclose(src);
return NULL; return NULL;
} }
@ -800,16 +801,21 @@ FILE *FSZIP_Deflate(FILE *src, qofs_t srcsize, qofs_t outsize)
inflateEnd(&strm); inflateEnd(&strm);
fclose(src); fclose(src);
fclose(of); fclose(of);
Con_Printf("Couldn't decompress file\n"); Con_Printf("Couldn't decompress file, corrupt?\n");
return NULL; return NULL;
} }
} }
fwrite(outbuffer, 1, strm.total_out, of); fwrite(outbuffer, 1, strm.total_out, of);
inflateEnd(&strm); inflateEnd(&strm);
fclose(src); 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); fseek(of, SEEK_SET, 0);
return of; return of;
#else #else

View file

@ -3120,10 +3120,11 @@ static void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype)
pheader->gltextures[i][0] = TexMgr_LoadImage (loadmodel, filename, fwidth, fheight, pheader->gltextures[i][0] = TexMgr_LoadImage (loadmodel, filename, fwidth, fheight,
fmt, data, filename, 0, TEXPREF_ALPHA|texflags|TEXPREF_MIPMAP ); fmt, data, filename, 0, TEXPREF_ALPHA|texflags|TEXPREF_MIPMAP );
//now try to load glow/luma image from the same place
if (malloced) if (malloced)
free(data); free(data);
Hunk_FreeToLowMark (mark); Hunk_FreeToLowMark (mark);
//now try to load glow/luma image from the same place
q_snprintf (filename2, sizeof(filename2), "%s_glow", filename); q_snprintf (filename2, sizeof(filename2), "%s_glow", filename);
data = !gl_load24bit.value?NULL:Image_LoadImage (filename2, &fwidth, &fheight, &fmt, &malloced); data = !gl_load24bit.value?NULL:Image_LoadImage (filename2, &fwidth, &fheight, &fmt, &malloced);
if (!data) if (!data)
@ -3137,6 +3138,10 @@ static void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype)
fmt, data, filename, 0, TEXPREF_ALPHA|texflags|TEXPREF_MIPMAP ); fmt, data, filename, 0, TEXPREF_ALPHA|texflags|TEXPREF_MIPMAP );
else else
pheader->fbtextures[i][0] = NULL; pheader->fbtextures[i][0] = NULL;
if (malloced)
free(data);
Hunk_FreeToLowMark (mark);
} }
else else
{ {

View file

@ -106,13 +106,17 @@ byte *Image_LoadPNG(FILE *f, int *width, int *height, qboolean *malloced)
in = malloc(com_filesize); in = malloc(com_filesize);
if (!in) if (!in)
{
fclose(f);
return NULL; return NULL;
}
if (com_filesize == fread(in, 1, com_filesize, f)) if (com_filesize == fread(in, 1, com_filesize, f))
{ {
*malloced = true; *malloced = true;
lodepng_decode32(&out, &w, &h, in, insize); lodepng_decode32(&out, &w, &h, in, insize);
} }
free(in); free(in);
fclose(f);
return out; return out;
#endif #endif
} }
@ -175,11 +179,11 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt
fread(&fmtheader, 1, sizeof(fmtheader), f); fread(&fmtheader, 1, sizeof(fmtheader), f);
if (fmtheader.magic != (('D'<<0)|('D'<<8)|('S'<<16)|(' '<<24))) if (fmtheader.magic != (('D'<<0)|('D'<<8)|('S'<<16)|(' '<<24)))
return NULL; goto fail;
fmtheader.dwSize += sizeof(fmtheader.magic); fmtheader.dwSize += sizeof(fmtheader.magic);
if (fmtheader.dwSize != sizeof(fmtheader)) if (fmtheader.dwSize != sizeof(fmtheader))
return NULL; //corrupt/different version goto fail; //corrupt/different version
memset(&fmt10header, 0, sizeof(fmt10header)); memset(&fmt10header, 0, sizeof(fmt10header));
fmt10header.arraysize = (fmtheader.ddsCaps[1] & 0x200)?6:1; //cubemaps need 6 faces... 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(" blue: %08x\n", fmtheader.ddpfPixelFormat.bluemask);
Con_Printf("alpha: %08x\n", fmtheader.ddpfPixelFormat.alphamask); 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); Con_Printf(" used: %08x\n", fmtheader.ddpfPixelFormat.redmask^fmtheader.ddpfPixelFormat.greenmask^fmtheader.ddpfPixelFormat.bluemask^fmtheader.ddpfPixelFormat.alphamask);
return NULL; goto fail;
} }
#undef IsPacked #undef IsPacked
} }
@ -426,7 +430,7 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt
default: default:
Con_Printf("Unsupported dds10 dxgi in %s - %u\n", fname, fmt10header.dxgiformat); 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 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)[1],
((char*)&fmtheader.ddpfPixelFormat.dwFourCC)[2], ((char*)&fmtheader.ddpfPixelFormat.dwFourCC)[2],
((char*)&fmtheader.ddpfPixelFormat.dwFourCC)[3]); ((char*)&fmtheader.ddpfPixelFormat.dwFourCC)[3]);
return NULL; goto fail;
} }
if ((fmtheader.ddsCaps[1] & 0x200) && (fmtheader.ddsCaps[1] & 0xfc00) != 0xfc00) 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 (fmtheader.ddsCaps[1] & 0x200)
{ {
if (fmt10header.arraysize % 6) //weird number of faces. if (fmt10header.arraysize % 6) //weird number of faces.
return NULL; goto fail;
if (fmt10header.arraysize == 6) if (fmt10header.arraysize == 6)
{ {
//layers = 6; //layers = 6;
return NULL; //don't try to load cubemaps. goto fail; //don't try to load cubemaps.
} }
else else
{ {
//layers = fmt10header.arraysize; //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) else if (fmtheader.ddsCaps[1] & 0x200000)
{ {
if (fmt10header.arraysize != 1) //no 2d arrays if (fmt10header.arraysize != 1) //no 2d arrays
return NULL; goto fail;
return NULL; //don't try to load 3d textures. goto fail; //don't try to load 3d textures.
} }
else else
{ {
if (fmt10header.arraysize == 1) if (fmt10header.arraysize == 1)
; //yay, we can load 2d images. ; //yay, we can load 2d images.
else else
return NULL; //don't try to load 2d arrays. goto fail; //don't try to load 2d arrays.
//layers = fmt10header.arraysize; //layers = fmt10header.arraysize;
} }
@ -485,22 +489,28 @@ static byte *Image_LoadDDS(FILE *f, int *width, int *height, enum srcformat *fmt
if (mipnum != nummips) if (mipnum != nummips)
{ {
Con_Printf("%s: dds with incomplete mip chain\n", fname); Con_Printf("%s: dds with incomplete mip chain\n", fname);
return NULL; goto fail;
} }
datasize = TexMgr_ImageSize(fmtheader.dwWidth, fmtheader.dwHeight, encoding); datasize = TexMgr_ImageSize(fmtheader.dwWidth, fmtheader.dwHeight, encoding);
if (!datasize) if (!datasize)
return NULL; //werid/unsupported goto fail; //werid/unsupported
//just read the mipchain into a new bit of memory and return that. //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. //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); ret = (byte *) Hunk_Alloc (datasize);
fread(ret, 1, datasize, f); fread(ret, 1, datasize, f);
fclose(f);
*width = fmtheader.dwWidth; *width = fmtheader.dwWidth;
*height = fmtheader.dwHeight; *height = fmtheader.dwHeight;
*fmt = encoding; *fmt = encoding;
return ret; return ret;
fail:
fclose(f);
return NULL;
} }
/*spike -- end of dds loader*/ /*spike -- end of dds loader*/