pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Davis <pgsql@j-davis.com>
To: pgsql-bugs@postgresql.org
Subject: init_htab causes SIGFPE (or worse) due to miscalculation for large nbuckets
Date: Sun, 09 Dec 2012 11:01:55 -0800
Message-ID: <1355079715.3896.14.camel@jdavis> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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).

The only simple way I can reproduce this is with gdb:

1. attach gdb to a session
2. set a breakpoint in ExecInitRecursiveUnion and continue
3. execute in session:
        set work_mem='100GB';
        with recursive r (i) as
          (select 1 union select i+1 from r where i < 10)
          select * from r;
4. (gdb) set node->numGroups = (1 << 30) + 1
5. (gdb) continue

I think we should just cap nbuckets at 1 << 30 in init_htab.

There was a previous fix here:

http://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=299d1716525c659f0e02840e31fbe4dea3

But it assumed that init_htab could deal with INT_MAX. In practice,
work_mem will usually be the limiting factor anyway, but not if it's set
high.

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



view thread (9+ messages)  latest in thread

Message-ID: <1355079715.3896.14.camel@jdavis>
Permalink:  ../1355079715.3896.14.camel@jdavis/
Also on:    postgresql.org/message-id/1355079715.3896.14.camel@jdavis

 · 

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: pgsql@j-davis.com
  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>

* 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