Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p7ftH-00030D-7w for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Dec 2022 16:54:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p7ftE-0007IX-0O for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Dec 2022 16:54:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p7ftD-0007IO-PO for pgsql-bugs@lists.postgresql.org; Tue, 20 Dec 2022 16:54:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p7ftB-0004uC-J3 for pgsql-bugs@lists.postgresql.org; Tue, 20 Dec 2022 16:54:50 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2BKGskIV1180065; Tue, 20 Dec 2022 11:54:46 -0500 From: Tom Lane To: Robins Tharakan cc: John Naylor , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17725: Sefault when seg_in() called with a large argument In-reply-to: References: <17725-0a09313b67fbe86e@postgresql.org> Comments: In-reply-to Robins Tharakan message dated "Tue, 20 Dec 2022 23:18:19 +1030" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1180063.1671555286.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 20 Dec 2022 11:54:46 -0500 Message-ID: <1180064.1671555286@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Robins Tharakan writes: > On Tue, 20 Dec 2022 at 20:44, John Naylor = wrote: >> Neither query shows the reported problem in my environment on master (a= s of today) or v14, so not sure > After trying a few combinations, I see that passing > CFLAGS=3D"-Wuninitialized" (default for my test setup) causes this failu= re. > Removing the flag gives the error you mention, and possibly why this > may not be easy to reproduce on a production system (unsure). I don't see a crash either, but I can't help observing that this input leads to a "seg" struct with "-46" significant digits: (gdb) p *seg $3 =3D {lower =3D 31, upper =3D 31, l_sigd =3D -46 '\322', u_sigd =3D -46 = '\322', = l_ext =3D 0 '\000', u_ext =3D 0 '\000'} So we're invoking sprintf with a fairly insane precision spec: 939 sprintf(result, "%.*e", n - 1, val); (gdb) p n $4 =3D -46 (gdb) p val $5 =3D 31 POSIX says "a negative precision is taken as if the precision were omitted", and our code seems to do that, but I wonder if this is managing to overrun the output buffer on your platform. IMO: 1. The seg grammar needs to constrain the result of significant_digits() to something that will fit in the allocated "char" field width. It looks like some code paths there have clamps, but not all. 2. Because we might already have stored "seg" values with bogus sigd values, restore() had better clamp the "n" value it's given to something sane. I see it clamps large positive values, but it's not worrying about zero-or-negative. regards, tom lane