Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bv7vR-0000AG-Pq for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Oct 2016 19:13:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bv7vR-0007Vm-Cj for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Oct 2016 19:13:49 +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 1bv7vQ-0007Vg-U3 for pgsql-hackers@postgresql.org; Fri, 14 Oct 2016 19:13:48 +0000 Received: from gauss.credativ.com ([93.94.130.89]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bv7vO-0004mV-FF for pgsql-hackers@postgresql.org; Fri, 14 Oct 2016 19:13:48 +0000 Received: from gauss.credativ.com (localhost [127.0.0.1]) by gauss.credativ.com (Postfix) with ESMTP id CBC28AF00DF; Fri, 14 Oct 2016 21:13:43 +0200 (CEST) Received: from ex (ip-109-44-3-236.web.vodafone.de [109.44.3.236]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: ase@gauss.credativ.com) by gauss.credativ.com (Postfix) with ESMTPSA id 80016AF002B for ; Fri, 14 Oct 2016 21:13:43 +0200 (CEST) From: Andreas Seltenreich 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> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.50 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Fix-possible-infinite-recursion-on-bitshift.patch From cfdc425f75da268e1c2af08f936c59f34b69e577 Mon Sep 17 00:00:00 2001 From: Andreas Seltenreich 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 --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --=-=-=--