Re: [AD] Zero sized bitmaps

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Monday 22 August 2005 12:49 pm, Evert Glebbeek wrote:
> It seems that there is a majority opinion that create_bitmap(0,0) should
> return NULL. Reportedly, create_bitmap() returns an invalid bitmap (as the
> docs imply it might) that causes a crash when destroy_bitmap() is called
> on it. Looking at the code, I can't for the life of me figure out why

I found the problem. I'm surprised, actually MSVC is behaving (more) 
correctly, as by all rights this should crash. the main BITMAP gets allocated 
like so:
bitmap = malloc(sizeof(BITMAP) + (sizeof(char *) * height));

However, right down below, the code sets this, regardless of the height of the 
bitmap:
bitmap->line[0] = bitmap->dat;

If height is 0, that means the line[] array is pointing outside the allocated 
memory space (since it allocated only the BITMAP struct, and none of the 
extra memory for the line[] array). A proper fix (if you don't want to return 
NULL), would simply be:

if (height>0) {
   bitmap->line[0] = bitmap->dat;
   for (i=1; i<height; i++)
      bitmap->line[i] = bitmap->line[i-1] + width *
                        BYTES_PER_PIXEL(color_depth);
}

Personally, I'm not sure if it should return NULL on a 0x0 bitmap anymore. I 
do think it should definitely return NULL when you try to create one of 
negative size (if either width or height is less than 0).. but whatever you 
wanna do with a bitmap of 0 is up to you. But if you make it return NULL for 
a 0-height bitmap, you should do it for a 0-width bitmap as well. The current 
code allows 0-width bitmap even though they're effectively unusable (and will 
crash if you try to dereference the line[] array's pointers or the dat 
field).




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/