Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiEd4-0001MU-Ms for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Dec 2012 01:27:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TiEd3-0004Lo-Rv for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Dec 2012 01:27:25 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiEd3-0004Lj-6w for pgsql-bugs@postgresql.org; Tue, 11 Dec 2012 01:27:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiEd2-0006bk-4n for pgsql-bugs@postgresql.org; Tue, 11 Dec 2012 01:27:24 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id qBB1RMUl001037; Mon, 10 Dec 2012 20:27:22 -0500 (EST) From: Tom Lane To: Jeff Davis cc: pgsql-bugs@postgresql.org Subject: Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets 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> Comments: In-reply-to Jeff Davis message dated "Mon, 10 Dec 2012 16:29:36 -0800" Date: Mon, 10 Dec 2012 20:27:22 -0500 Message-ID: <1036.1355189242@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Jeff Davis 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