I have a function about image feature, when I malloc a buffer (buffer size via read header). The fortify report tell me "Integer Overflow" in here. But, whether I fix code or check value of colors, fortify report still tell me "Integer Overflow"
Anyone have any suggestion?
code:
int ReadInt()
{
int rnt=0;
rnt = getc(xxx);
rnt += (getc(xxx)<<8);
rnt += (getc(xxx)<<16);
rnt += (getc(xxx)<<24);
return rnt;
}
int image()
{
....
image->header_size=ReadInt();
image->width=ReadInt();
image->height=ReadInt();
....
image->colors =ReadInt();
int unit_size = 0;
unit_size = sizeof(unsigned int);
unsigned int malloc_size = 0;
if (image->colors > 0 &&
image->colors < (1024 * 1024 * 128) &&
unit_size > 0 &&
unit_size <= 8)
{
malloc_size = (image->colors * unit_size);
image->palette = (unsigned int *)malloc( malloc_size );
}
....
return 0;
}
fortift report:
Abstract: The function image() in xzy.cpp does not account for
integer overflow, which can result in a logic error or a buffer overflow.
Source: _IO_getc()
59 rnt += (getc(xxx)<<8);
60 rnt += (getc(xxx)<<16);
61 rnt += (getc(xxx)<<24);
62 return rnt;
Sink: malloc()
242 malloc_size = (image->colors * unit_size);
243 image->palette = (unsigned int *)malloc( malloc_size );
244