Re: gwlist_sort bug fix

Alexander Malysh <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
Hi,

yep, you are right. But I think it's better to implement our own qsort
instead of moving list elements. List might be very large...

[email protected] wrote:

> Hello,
> 
> Nobody uses gwlist_sort() but insect found. So I will describe it and
> propose fix for benefit of future generations.
> 
> Forementioned function uses qsort(3) like this:
> 
>>    qsort(&GET(list, 0), list->len, sizeof(void*), cmp);
> 
> And remember this code:
> 
>> #define INDEX(list, i)  (((list)->start + i) % (list)->tab_size)
>> #define GET(list, i)    ((list)->tab[INDEX(list, i)])
> 
> If tab array is wrapped this produces misbehavior. For example if we have
> tab_size = 7, len = 7, start = 1 qsort() will try to sort 7 elements
> starting from second, which mean it will try to pass into compare function
> bogus element past end of tab array, resulting crash or precious software.
> 
> This probably should fix the problem:
> 
>     // if array is wrapped we need to merge parts into a single chunk
>     if ((list->start + list->len) > list->tab_size) {
>         if (list->len != list->tab_size)
>             memmove(list->tab + INDEX(list, list->len),
>                     list->tab + list->start,
>                     (list->len - INDEX(list, list->len)) * sizeof(void
>                     *));
>         list->start = 0;
>     }
> 
>     qsort(&GET(list, 0), list->len, sizeof(void*), cmp);
> 
> 
> Best regards.

-- 
Thanks,
Alex
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.