Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiDx5-0000hw-Mz for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Dec 2012 00:44:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TiDx4-0007Qm-GL for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Dec 2012 00:44:02 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiDx3-0007Qh-RJ for pgsql-bugs@postgresql.org; Tue, 11 Dec 2012 00:44:01 +0000 Received: from mail-pb0-f46.google.com ([209.85.160.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TiDx2-0005z2-1U for pgsql-bugs@postgresql.org; Tue, 11 Dec 2012 00:44:00 +0000 Received: by mail-pb0-f46.google.com with SMTP id wy7so2281288pbc.19 for ; Mon, 10 Dec 2012 16:43:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=subject:from:to:cc:in-reply-to:references:content-type:date :message-id:mime-version:x-mailer:content-transfer-encoding :x-gm-message-state; bh=ff3I1t4MEljFiIHkcZ8GHu3u4PR4SOcEwzBZeadgfE4=; b=Bezp+WL4YTTqcEo2CeWZD1jFq3x6bcASU9+BKnd5z4xpiG2IHZEvc1ozzcAtG8ibSM ZnSNxsVJB5h+Ygx0b6Xzd0ocLlVfbkX1WS+aCInyX9E1GlRoDTCNlyYPF/MQ+Yvhezcq cygIF9dUYdZO7Lv2z/qlJmIVUlqBdDU2oiaDYreaJPRSiFWjzZB/LTZ+70V+nZNCBoVo Oo9Z7CWo5iBZeScGW44b1Q8hsAphewRqK1DTV5olPazdHYkgMFM4usZpEK7npFdjwgC7 B1mfCVcQTAG+0Y2IU50Q2eRj2/8apGp9dKJKUvfxOCMbjsXdlPgwtl9zxvKF6l+twoBV 0+NQ== Received: by 10.68.230.200 with SMTP id ta8mr43241645pbc.13.1355186638663; Mon, 10 Dec 2012 16:43:58 -0800 (PST) Received: from ?IPv6:::1? ([2600:3c01::f03c:91ff:fe93:2ae4]) by mx.google.com with ESMTPS id o6sm12757440paw.37.2012.12.10.16.43.56 (version=SSLv3 cipher=OTHER); Mon, 10 Dec 2012 16:43:57 -0800 (PST) Subject: Re: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets From: Jeff Davis To: Tom Lane Cc: pgsql-bugs@postgresql.org In-Reply-To: <18018.1355093132@sss.pgh.pa.us> References: <1355079715.3896.14.camel@jdavis> <18018.1355093132@sss.pgh.pa.us> Content-Type: text/plain; charset="ISO-8859-1" Date: Mon, 10 Dec 2012 16:29:36 -0800 Message-ID: <1355185776.13316.4.camel@sussancws0025> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQmjYYQUQKdryp4dbXC5XD4KYxQd6euUh9wjBn5Hdz7QpMgAc+JsMJRT8gU32eFynZ0P0Jaf X-Pg-Spam-Score: -2.6 (--) 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 On Sun, 2012-12-09 at 17:45 -0500, Tom Lane wrote: > 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. 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). Then, the caller can bound the result further if needed, which should be less error-prone, because the caller would see that it returns a long (and compiler, but I don't seem to get a warning for implicit long->int conversions). Both of the callers that actually want a log2 already assume that the input is a power of two, so we can redefine my_log2 to require it. Regards, Jeff Davis -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs