I am writing a function that returns the address of the first located pixel with specified color. Right now i am testing the detection. Currently on 50x50 bmp. Considering this code:
dword bmp_find_xy (dword xp, dword yp)
{
dword w = 50;
word bpx = (3*8);
dword offset = (2+sizeof(BMP)+sizeof(DIB));
dword row = (((bpx * w) * 4) / 32);
dword pixAddress = (offset) + row * yp + ((xp * bpx) / 8);
return pixAddress;
}
dword bmp_dfind_c (char *FILE_NAME, BYTE R, BYTE G, BYTE B)
{
dword w = 50;
dword h = 50;
dword size;
int W, H, i;
FILE* fp = fopen("sample.bmp", "r+b");
BYTE* bmp;
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
bmp = malloc(size+48); // note this line
rewind(fp);
for(i=0; i<size; i++)
bmp[i] = fgetc(fp);
fseek(fp, 54, SEEK_SET);
for(H = h; H >=1; H--)
{
for(W = 0; W < w; W++)
{
if(bmp[bmp_find_xy(FILE_NAME, W, H)] == 255)
printf("There is a pix with maxed value");
}
}
fclose(fp);
return 1;
}
So.. since im using ancient compiler and there are no optimizations.. im receiving error for buffer overflow if i don't put at least +48 to size. Why 48? Why it overflows if i put only malloc(size)
that makes no sense to me.