Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dVKo0-0008Fj-7J for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Jul 2017 16:48:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dVKnz-0000iy-OC for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Jul 2017 16:48:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dVKnI-0007ud-IR for pgsql-bugs@postgresql.org; Wed, 12 Jul 2017 16:47:20 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dVKnF-0001Ac-Mj for pgsql-bugs@postgresql.org; Wed, 12 Jul 2017 16:47:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v6CGkuxc015155; Wed, 12 Jul 2017 12:46:56 -0400 From: Tom Lane To: Heikki Linnakangas cc: Peter Geoghegan , Andres Freund , Sergey Koposov , "pgsql-bugs@postgresql.org" Subject: Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow In-reply-to: <007e3020-3d27-8ac8-7269-9db7ebe9a8bf@iki.fi> References: <20170629161637.1478.93109@wrigleys.postgresql.org> <1499281579.8258.32.camel@cmu.edu> <28906.1499292236@sss.pgh.pa.us> <20170705221414.bgnw73z2mwkjwor7@alap3.anarazel.de> <3de75af4-a49f-36d1-347f-f170693ce6f5@iki.fi> <4817.1499874124@sss.pgh.pa.us> <8081.1499876094@sss.pgh.pa.us> <007e3020-3d27-8ac8-7269-9db7ebe9a8bf@iki.fi> Comments: In-reply-to Heikki Linnakangas message dated "Wed, 12 Jul 2017 19:20:53 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <15153.1499878016.1@sss.pgh.pa.us> Date: Wed, 12 Jul 2017 12:46:56 -0400 Message-ID: <15154.1499878016@sss.pgh.pa.us> 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 Heikki Linnakangas writes: > On 07/12/2017 07:14 PM, Tom Lane wrote: >> Uh ... what assumption? That's certainly true on any twos-complement >> machine. Besides, if you're worried about hypothetical portability >> issues, ... > Right, it's a hypothetical portability issue. The assumption we're > making is that UINT_MAX >= INT_MAX * 2 + 1. I'm not aware of any system > where it's not true, but I don't know what the C standards say about that. Actually, at the top of the loop we have i < n <= INT_MAX, so the assumption only needs to be UINT_MAX >= INT_MAX * 2 - 1. My copy of the C99 draft says, among other things 6.2.5 Types [#6] For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements. [#9] The range of nonnegative values of a signed integer type is a subrange of the corresponding unsigned integer type, and the representation of the same value in each type is the same. 6.2.6.2 Integer types [#1] For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N-1, so that objects of that type shall be capable of representing values from 0 to 2N-1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.37) [#2] For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; there shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M<=N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, then the value shall be modified in one of the following ways: -- the corresponding value with sign bit 0 is negated; -- the sign bit has the value -2N; -- the sign bit has the value 1-2N. [#3] The values of any padding bits are unspecified. Both 6.2.5.9 and 6.2.6.2.2 constrain INT_MAX to be <= UINT_MAX. In principle, you could have a conforming implementation in which they were the same, and the sign bit of a signed integer was treated as a useless padding bit in an unsigned integer. I can't imagine anyone actually building it that way though; what would be the point? But as long as there aren't wasted bits in an unsigned int, these rules require INT_MAX to be <= UINT_MAX/2, AFAICS. 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