Skip to content

Commit

Permalink
Check fread return values in Image_LoadLMP
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-drexler committed Aug 10, 2024
1 parent d82fea9 commit e41b2a5
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions Quake/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,23 +337,34 @@ static byte *Image_LoadLMP (FILE *f, int *width, int *height)
{
lmpheader_t qpic;
size_t pix;
int mark;
void *data;

fread(&qpic, sizeof(qpic), 1, f);
if (fread (&qpic, sizeof(qpic), 1, f) != 1)
{
fclose (f);
return NULL;
}
qpic.width = LittleLong (qpic.width);
qpic.height = LittleLong (qpic.height);

pix = qpic.width*qpic.height;

if (com_filesize != 8+pix)
if (com_filesize != sizeof (qpic) + pix)
{
fclose(f);
fclose (f);
return NULL;
}

data = (byte *) Hunk_Alloc(pix); //+1 to allow reading padding byte on last line
fread(data, 1, pix, f);
fclose(f);
mark = Hunk_LowMark ();
data = (byte *) Hunk_Alloc (pix);
if (fread (data, 1, pix, f) != pix)
{
Hunk_FreeToLowMark (mark);
fclose (f);
return NULL;
}
fclose (f);

*width = qpic.width;
*height = qpic.height;
Expand Down

0 comments on commit e41b2a5

Please sign in to comment.