Re: [AD] Zero sized bitmaps |
[ Thread Index |
Date Index
| More lists.liballeg.org/allegro-developers Archives
]
- To: alleg-developers@xxxxxxxxxx
- Subject: Re: [AD] Zero sized bitmaps
- From: Chris <chris.kcat@xxxxxxxxxx>
- Date: Mon, 22 Aug 2005 14:02:30 -0700
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:from:to:subject:date:user-agent:references:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:message-id; b=TCjEA8UBXt0gytQa2+q5YdUWl/VFVL2GqGmMvY1oQw4mYECYSkrbCbbAR2xFa7DQbxm3qoPz6HYvq3VlVikJ3BSiUxIPBphfFSU9a8hO6R2//3OGacXNTf3mOcShp6v7CvSCD1jQZVDQ7zBQCBPbLUxXPyJO7lOz6+wA1p/+DBo=
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).