while or for ?

John (J5) Palmieri johnp at redhat.com
Tue Oct 17 06:57:43 PDT 2006

On Tue, 2006-10-17 at 10:22 +0200, frederic heem wrote:
> Hi,
> While hinting a memory leak, I came across a weird construct which leads to a 
> bug:
>   /* Find empty slot */
>   i = 0;
>   while (message_cache[i] != NULL)
>     ++i;
> when the message_cache array is full, the index i will be out of range, hence 
> message_cache[i] will point where it can't.
> IMHO, a for loop shall be used in this case.
> Frederic Heem

Hi Frederic,

Thanks for the testing.  It would be nice if you gave a file and
function next time so it is easier for us to find the portion of code
you are looking at.  As for this portion which I believe is in
dbus/dbus-message.c(dbus_message_cache_or_finalize) you will notice the
line above states:

if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
    goto out;

this ensures that we will not read beyond the allocated size of the
array.  Thanks for looking into this.  Keep your eyes open as there very
well may be places where there are actual mistakes and having more eyes
on the code will help us find them.

John (J5) Palmieri <johnp at redhat.com>

More information about the dbus mailing list