pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Peter Geoghegan <pg@bowt.ie>
Cc: Andres Freund <andres@anarazel.de>
Cc: Sergey Koposov <skoposov@cmu.edu>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow
Date: Wed, 12 Jul 2017 12:46:56 -0400
Message-ID: <15154.1499878016@sss.pgh.pa.us> (raw)
In-Reply-To: <007e3020-3d27-8ac8-7269-9db7ebe9a8bf@iki.fi>
References: <20170629161637.1478.93109@wrigleys.postgresql.org>
	<CAH2-Wz=wjX9ADNR6V6eoTqC0J0FZo+VxmU2PPryMJKjZW=8wxg@mail.gmail.com>
	<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>
	<CAH2-WzkNs03GLMnv6pQZY8f2vKgjhsjmKByfQX6E27HCpjhKow@mail.gmail.com>
	<8081.1499876094@sss.pgh.pa.us>
	<007e3020-3d27-8ac8-7269-9db7ebe9a8bf@iki.fi>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Heikki Linnakangas <hlinnaka@iki.fi> 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



view thread (26+ messages)  latest in thread

Message-ID: <15154.1499878016@sss.pgh.pa.us>
Permalink:  ../15154.1499878016@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/15154.1499878016@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, hlinnaka@iki.fi, pg@bowt.ie, andres@anarazel.de, skoposov@cmu.edu
  Subject: Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow
  In-Reply-To: <15154.1499878016@sss.pgh.pa.us>
  References: <20170629161637.1478.93109@wrigleys.postgresql.org> <CAH2-Wz=wjX9ADNR6V6eoTqC0J0FZo+VxmU2PPryMJKjZW=8wxg@mail.gmail.com> <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> <CAH2-WzkNs03GLMnv6pQZY8f2vKgjhsjmKByfQX6E27HCpjhKow@mail.gmail.com> <8081.1499876094@sss.pgh.pa.us> <007e3020-3d27-8ac8-7269-9db7ebe9a8bf@iki.fi> <15154.1499878016@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