pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jeff Davis <pgsql@j-davis.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets
Date: Mon, 10 Dec 2012 20:27:22 -0500
Message-ID: <1036.1355189242@sss.pgh.pa.us> (raw)
In-Reply-To: <1355185776.13316.4.camel@sussancws0025>
References: <1355079715.3896.14.camel@jdavis>
	<18018.1355093132@sss.pgh.pa.us>
	<1355185776.13316.4.camel@sussancws0025>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (9+ messages)  latest in thread

Message-ID: <1036.1355189242@sss.pgh.pa.us>
Permalink:  ../1036.1355189242@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1036.1355189242@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql@j-davis.com
  Subject: Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets
  In-Reply-To: <1036.1355189242@sss.pgh.pa.us>
  References: <1355079715.3896.14.camel@jdavis> <18018.1355093132@sss.pgh.pa.us> <1355185776.13316.4.camel@sussancws0025> <1036.1355189242@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox