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 1oIChW-0000gV-3V for pgsql-hackers@arkaria.postgresql.org; Sun, 31 Jul 2022 17:26:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oIChT-0004WV-DD for pgsql-hackers@arkaria.postgresql.org; Sun, 31 Jul 2022 17:25:59 +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 1oIChT-0004WF-3l for pgsql-hackers@lists.postgresql.org; Sun, 31 Jul 2022 17:25:59 +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 1oIChJ-00064k-Nr for pgsql-hackers@lists.postgresql.org; Sun, 31 Jul 2022 17:25:55 +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 26VHPXOx1233972; Sun, 31 Jul 2022 13:25:34 -0400 From: Tom Lane To: Nathan Bossart cc: Martin Kalcher , PostgreSQL Hackers Subject: Re: [Patch] Fix bounds check in trim_array() In-reply-to: <20220728224618.GA412680@nathanxps13> References: <20220728224618.GA412680@nathanxps13> Comments: In-reply-to Nathan Bossart message dated "Thu, 28 Jul 2022 15:46:18 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1233970.1659288333.1@sss.pgh.pa.us> Date: Sun, 31 Jul 2022 13:25:33 -0400 Message-ID: <1233971.1659288333@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nathan Bossart writes: > On Mon, Jul 25, 2022 at 04:40:51PM +0200, Martin Kalcher wrote: >> +SELECT trim_array(ARRAY[]::int[], 1); -- fail >> +ERROR: number of elements to trim must be between 0 and 0 > Can we improve the error message? Maybe it should look something like > ERROR: number of elements to trim must be 0 > for this case. Hmm, I'm unexcited about making our long-suffering translators deal with another translatable string for such a corner case. I think it's fine as-is. A bigger problem is that a little further down, there's an equally unprotected reference to ARR_LBOUND(v)[0]. Now, the fact that that expression computes garbage doesn't matter too much, because AFAICS if the array is zero-D then array_get_slice is going to exit at if (ndim < nSubscripts || ndim <= 0 || ndim > MAXDIM) return PointerGetDatum(construct_empty_array(elemtype)); without ever examining its upperIndx[] argument. However, once we put in a test case covering this behavior, I bet that valgrind-using buildfarm animals will start to bleat about the invalid memory access. I think the easiest fix is like if (ARR_NDIM(v) > 0) { upper[0] = ARR_LBOUND(v)[0] + array_length - n - 1; upperProvided[0] = true; } It'd be good to get this fix into next week's minor releases, so I'll go push it. regards, tom lane