Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vkq6O-00039r-GG for pgsql-bugs@arkaria.postgresql.org; Mon, 25 Nov 2013 06:57:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Vkq6N-0001iZ-HQ for pgsql-bugs@arkaria.postgresql.org; Mon, 25 Nov 2013 06:56:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vkq6M-0001hY-ET for pgsql-bugs@postgresql.org; Mon, 25 Nov 2013 06:56:58 +0000 Received: from mimolette.dalibo.net ([212.85.154.222]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Vkq6K-0007f9-3m for pgsql-bugs@postgresql.org; Mon, 25 Nov 2013 06:56:58 +0000 Received: from ronan_laptop.localnet (unknown [78.193.41.71]) by mimolette.dalibo.net (Postfix) with ESMTPSA id F23D2682001D; Mon, 25 Nov 2013 07:56:21 +0100 (CET) From: Ronan Dunklau To: Tom Lane Cc: Heikki Linnakangas , pgsql-bugs Subject: Re: Server may segfault when using slices on int2vector Date: Mon, 25 Nov 2013 07:56:21 +0100 Message-ID: <4748023.maNicuM2Tz@ronan_laptop> User-Agent: KMail/4.11.3 (Linux/3.12.1-gentoo; KDE/4.11.3; x86_64; ; ) In-Reply-To: <20939.1385239179@sss.pgh.pa.us> References: <2450353.1U6Qoyzd8o@ronan_laptop> <528CA074.4030307@vmware.com> <20939.1385239179@sss.pgh.pa.us> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Le samedi 23 novembre 2013 15:39:39 Tom Lane a =E9crit : > Heikki Linnakangas writes: > > On 19.11.2013 16:24, Ronan Dunklau wrote: > >> [ this crashes: ] > >> select > >> a.indkey[1:3], > >> a.indkey[1:2] > >> from pg_index as a > >=20 > > I don't think it's safe to allow slicing int2vectors (nor oidvectors). > > It seems all too likely that the result violates the limitations of > > int2vector. In addition to that segfault, the array returned is 1-based, > > not 0-based as we assume for int2vectors. One consequence of that is > > that if you COPY the value out in binary format and try to read it back, > > you'll get an error. > >=20 > > So I think we should just not allow slicing oidvectors, and throw an > > error. You can cast from int2vector to int2[], and slice and dice that > > as much as you want, so it's not a big loss in functionality. >=20 > I think more useful is to automatically cast up to int2[], rather than > make the user write something as ugly as "(a.indkey::int2[])[1:3]". > This case is really pretty similar to what we have to do when handling > domains over arrays; int2vector could be thought of as a domain over > int2[] that constrains the allowed subscripting. And what we do for > those is automatically cast up. >=20 > With that thought in mind, I tried tweaking transformArrayType() to > auto-cast int2vector and oidvector to int2[] and oid[]. That resolves > this crash without throwing an error. On the other hand, it causes > assignment to an element or slice of these types to throw an error, which > strikes me as a Good Thing because otherwise such an assignment could > likewise result in a value violating the subscript constraints of these > types. We could if we liked fix that by providing a cast from int2[] to > int2vector that checks the subscript constraints, but like you I'm not > thinking it's worth the trouble. There are certainly no cases in the > system catalogs where we want to support such manual assignments. >=20 > While testing that I discovered an independent bug in > transformAssignmentSubscripts: the "probably shouldn't fail" error > reporting code doesn't work because "result" has already been set to NULL. > We should fix that as per attached whether or not we choose to resolve > Ronan's bug this way. >=20 > The attached patch is just a quick draft for testing; it needs more work > on the nearby comments, and the OIDs of int2[] and oid[] should be named > by #define's in pg_type.h not by literal constants. If there are no > objections, I'll clean it up and commit. >=20 > regards, tom lane Thank you for the match, I saw it got commited. I have some more questions, however not directly related to this bug. Casting from int2vector to int2[] returns a zero-indexed array, but slicing= an=20 int2vector returns a one-indexed array. This behavior is consistent with=20 slicing in general, which always returns 1 indexed arrays, but I found it=20 surprising, especially when writing a query like this: select ('1 2'::int2vector)[0:1] =3D ('1 2'::int2vector)::int2[]; I would have expected to return true, but it actually isn't because one arr= ay=20 is zero-indexed and the other one is not. Is there a more convenient way to change the lower bound of an array than t= o=20 slice it ? Thank you very much. --=20 Ronan Dunklau http://dalibo.com - http://dalibo.org --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs