Re: [PATCH v4 2/2] bsearch: prevent overflow when computing middle comparison element

From: Thiago Farina
Date: Wed Nov 11 2009 - 10:28:34 EST


On Wed, Nov 11, 2009 at 1:09 PM, Thiago Farina <tfransosi@xxxxxxxxx> wrote:
> On Tue, Nov 10, 2009 at 1:00 PM, Andrà Goddard Rosa
> <andre.goddard@xxxxxxxxx> wrote:
>> Âvoid *bsearch(const void *key, const void *base, size_t num, size_t size,
>> Â Â Â Â Â Â Âint (*cmp)(const void *key, const void *elt))
>> Â{
>> - Â Â Â int start = 0, end = num, mid, result;
>> + Â Â Â size_t start = 0, end = num;
>> + Â Â Â int result;
>>
>> Â Â Â Âwhile (start < end) {
>> - Â Â Â Â Â Â Â mid = (start + end) / 2;
>> + Â Â Â Â Â Â Â size_t mid = start + (end - start) / 2;
> I think it is more readable if you could keep the mid variable outside
> of the while loop.
>
I mean: size_t start = 0, end = num, mid;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/