Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Thpcu-0003RM-DY for pgsql-bugs@arkaria.postgresql.org; Sun, 09 Dec 2012 22:45:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Thpct-00033q-QW for pgsql-bugs@arkaria.postgresql.org; Sun, 09 Dec 2012 22:45:35 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Thpct-00033d-6l for pgsql-bugs@postgresql.org; Sun, 09 Dec 2012 22:45:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Thpcr-0000Sz-RS for pgsql-bugs@postgresql.org; Sun, 09 Dec 2012 22:45:34 +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 qB9MjW2E018019; Sun, 9 Dec 2012 17:45:32 -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: <1355079715.3896.14.camel@jdavis> References: <1355079715.3896.14.camel@jdavis> Comments: In-reply-to Jeff Davis message dated "Sun, 09 Dec 2012 11:01:55 -0800" Date: Sun, 09 Dec 2012 17:45:32 -0500 Message-ID: <18018.1355093132@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: > If nbuckets is around 2^30+1, which can happen if work_mem is very high, > then: > nbuckets = 1 << my_log2(lnbuckets); > ends up as 1 << 31, which is negative, leading to a SIGFPE on my > machine, but I think it can also lead to an infinite loop or a crash > (after corrupting the HASHHDR). > ... > I think we should just cap nbuckets at 1 << 30 in init_htab. Yeah. After looking at other uses of my_log2, it seems like hash_estimate_size and hash_select_dirsize probably should also bound their inputs to avoid overflow of 1 << my_log2() calculations. Alternatively, maybe we should hack my_log2 to do that bounding. As-is, it seems like trouble waiting to happen. This won't protect init_htab, which needs the shift result to fit in int not long. But my_log2 is just plain broken for inputs larger than LONG_MAX/2, anyway. 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