Re: Bug in garray.c



Sebastian Wilhelmi <wilhelmi@ira.uka.de> writes:

> Take a 'zero-terminated' (and/or 'clear'-ed) array. Append two non-null
> elements. Set the size to 0. Then set the size to 1. Now element 0 and element
> 1 are non-zero, thus making this array not zero-terminated nor cleared.
> 
> Appended is a patch to solve that. Furthermore I added some macros
> (g_array_elt_pos and g_array_elt_len) to make the raw memory accesing code a
> bit more readable.

The real question is: what is zero-terminated and cleared supposed to
mean?

Currently, zero-terminated means something like

  The array will be zero-terminated if it is not empty and its size is
  not made smaller with g_array_set_size.

This is clearly not appropriate. Your patch will make zero-terminated
mean

  The array is zero-terminated if it is not empty.

In my opinion, zero-terminated should either be depreceated or mean

  Thea array will be zero-terminated at all times, even when it is
  empty.

Zero-termination makes sense only in constructions like

SomeType *p

for (p = (SomeType *)array->data; *p; p++)
  {
    do_something (p);
  }

Note that ANSI C does not guarantee that the above works if SomeType
is a pointer and the zero-termination is done with memset
(array->data, somewhere, 0). A NULL pointer does not have to be the
address 0x0. 

Thus, zero-termination makes sense only for the various int types.


Currently, cleared means something like

Every time space for the array is allocated, it will be cleared to zero.

I simply don't see the point. Possibly, for debugging purposes, it may
make sense to have cleared mean:

  Allocated space that is not part of the array is zero


I think the bug is in the definition of the data type. This should be
fixed before the implementation.

> The same applies to GPtrArray, which is not supposed to be zero_terminated but
> should be cleared. So I removed the final 
>    
>    array->pdata[array->len - 1] = NULL;

NULL terminated (as opposed to a 0x0 terminated/cleared) arrays could
help catch off-by-one bugs.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]