Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets
Дата
Msg-id 1036.1355189242@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets  (Jeff Davis <pgsql@j-davis.com>)
Ответы Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets  (Jeff Davis <pgsql@j-davis.com>)
Список pgsql-bugs
Jeff Davis <pgsql@j-davis.com> writes:
> It looks like all of the callers, except two, immediately shift the
> result. So perhaps it would be better to make a new function (something
> like "ceil_pow2") that returns the lowest power of two greater than or
> equal to the input, and it can return a long (bounded to +LONG_MAX).

That does seem like a good idea.  We need one for an int-sized result
too, to fix the original problem in init_htab.  So I propose these
functions:

/* calculate ceil(log base 2) of num */
int
my_log2(long num)
{
    int            i;
    long        limit;

    /* guard against too-large input, which would put us into infinite loop */
    if (num > LONG_MAX / 2)
        num = LONG_MAX / 2;

    for (i = 0, limit = 1; limit < num; i++, limit <<= 1)
        ;
    return i;
}

/* calculate first power of 2 >= num, bounded to what will fit in a long */
long
next_power_of_two_long(long num)
{
    /* my_log2's internal range check is sufficient */
    return 1L << my_log2(num);
}

/* calculate first power of 2 >= num, bounded to what will fit in an int */
int
next_power_of_two_int(long num)
{
    if (num > INT_MAX / 2)
        num = INT_MAX / 2;
    return 1 << my_log2(num);
}


            regards, tom lane

В списке pgsql-bugs по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets
Следующее
От: Jeff Davis
Дата:
Сообщение: Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets