pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Seltenreich <seltenreich@gmx.de>
To: pgsql-hackers@postgresql.org
Subject: [sqlsmith] Infinite recursion in bitshift
Date: Fri, 14 Oct 2016 21:13:44 +0200
Message-ID: <87d1j2zqtz.fsf@credativ.de> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

Hi,

sqlsmith just found another crasher:

    select bit '1' >> (-2^31)::int;

This is due to an integer overflow in bitshiftright()/bitshiftleft()
leading to them recursively calling each other.  Patch attached.

regards,
Andreas

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments:

0001-Fix-possible-infinite-recursion-on-bitshift.patch (text/x-diff, 1.5K) download From cfdc425f75da268e1c2af08f936c59f34b69e577 Mon Sep 17 00:00:00 2001 From: Andreas Seltenreich <seltenreich@gmx.de> Date: Fri, 14 Oct 2016 20:52:52 +0200 Subject: [PATCH] Fix possible infinite recursion on bitshift. bitshiftright() and bitshiftleft() would recursively call each other infinitely when the user passed a MIN_INT for the shift amount due to an integer overflow. This patch reduces a negative shift amount to -VARBITMAXLEN to avoid overflow. --- src/backend/utils/adt/varbit.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/backend/utils/adt/varbit.c b/src/backend/utils/adt/varbit.c index 75e6a46..d8ecfb6 100644 --- a/src/backend/utils/adt/varbit.c +++ b/src/backend/utils/adt/varbit.c @@ -1387,9 +1387,15 @@ bitshiftleft(PG_FUNCTION_ARGS) /* Negative shift is a shift to the right */ if (shft < 0) + { + /* Protect against overflow */ + if (shft < -VARBITMAXLEN) + shft = -VARBITMAXLEN; + PG_RETURN_DATUM(DirectFunctionCall2(bitshiftright, VarBitPGetDatum(arg), Int32GetDatum(-shft))); + } result = (VarBit *) palloc(VARSIZE(arg)); SET_VARSIZE(result, VARSIZE(arg)); @@ -1447,9 +1453,15 @@ bitshiftright(PG_FUNCTION_ARGS) /* Negative shift is a shift to the left */ if (shft < 0) + { + /* Protect against overflow */ + if (shft < -VARBITMAXLEN) + shft = -VARBITMAXLEN; + PG_RETURN_DATUM(DirectFunctionCall2(bitshiftleft, VarBitPGetDatum(arg), Int32GetDatum(-shft))); + } result = (VarBit *) palloc(VARSIZE(arg)); SET_VARSIZE(result, VARSIZE(arg)); -- 2.9.3

view thread (6+ messages)  latest in thread

Message-ID: <87d1j2zqtz.fsf@credativ.de>
Permalink:  ../87d1j2zqtz.fsf@credativ.de/
Also on:    postgresql.org/message-id/87d1j2zqtz.fsf@credativ.de

 ·  · 

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-hackers@postgresql.org
  Cc: seltenreich@gmx.de
  Subject: Re: [sqlsmith] Infinite recursion in bitshift
  In-Reply-To: <87d1j2zqtz.fsf@credativ.de>
  References: <87d1j2zqtz.fsf@credativ.de>

* 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