From: Martin Kalcher <martin.kalcher@aboutsource.net>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: [Patch] Fix bounds check in trim_array()
Date: Mon, 25 Jul 2022 16:40:51 +0200
Message-ID: <baaeb413-b8a8-4656-5757-ef347e5ec11f@aboutsource.net> (raw)
Hi,
while working on something else i encountered a bug in the trim_array()
function. The bounds check fails for empty arrays without any
dimensions. It reads the size of the non existing first dimension to
determine the arrays length.
select trim_array('{}'::int[], 10);
------------
{}
select trim_array('{}'::int[], 100);
ERROR: number of elements to trim must be between 0 and 64
The attached patch fixes that check.
Martin
Attachments:
[text/x-patch] 0001-Fix-bounds-check-in-trim_array.patch (1.9K, ../baaeb413-b8a8-4656-5757-ef347e5ec11f@aboutsource.net/2-0001-Fix-bounds-check-in-trim_array.patch)
download | inline diff:
From b6173a8f8f94cddd5347db482b8e4480c0e546e7 Mon Sep 17 00:00:00 2001
From: Martin Kalcher <martin.kalcher@aboutsource.net>
Date: Mon, 25 Jul 2022 16:26:14 +0200
Subject: [PATCH] Fix bounds check in trim_array()
The bounds check in trim_array() failed for empty arrays without any
dimension. It read the size of the first dimension without checking its
existence.
---
src/backend/utils/adt/arrayfuncs.c | 2 +-
src/test/regress/expected/arrays.out | 2 ++
src/test/regress/sql/arrays.sql | 1 +
3 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.cindex fb167f226a..0342ebf4fd 100644--- a/src/backend/utils/adt/arrayfuncs.c+++ b/src/backend/utils/adt/arrayfuncs.c@@ -6839,7 +6839,7 @@ trim_array(PG_FUNCTION_ARGS)
{
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
int n = PG_GETARG_INT32(1);
- int array_length = ARR_DIMS(v)[0];+ int array_length = (ARR_NDIM(v) > 0) ? ARR_DIMS(v)[0] : 0;
int16 elmlen;
bool elmbyval;
char elmalign;
diff --git a/src/test/regress/expected/arrays.out b/src/test/regress/expected/arrays.outindex ce6f3a65f9..97920f38c2 100644--- a/src/test/regress/expected/arrays.out+++ b/src/test/regress/expected/arrays.out@@ -2445,3 +2445,5 @@ SELECT trim_array(ARRAY[1, 2, 3], -1); -- fail
ERROR: number of elements to trim must be between 0 and 3
SELECT trim_array(ARRAY[1, 2, 3], 10); -- fail
ERROR: number of elements to trim must be between 0 and 3
+SELECT trim_array(ARRAY[]::int[], 1); -- fail+ERROR: number of elements to trim must be between 0 and 0diff --git a/src/test/regress/sql/arrays.sql b/src/test/regress/sql/arrays.sqlindex f774faf856..791af5c0ce 100644--- a/src/test/regress/sql/arrays.sql+++ b/src/test/regress/sql/arrays.sql@@ -754,3 +754,4 @@ FROM
SELECT trim_array(ARRAY[1, 2, 3], -1); -- fail
SELECT trim_array(ARRAY[1, 2, 3], 10); -- fail
+SELECT trim_array(ARRAY[]::int[], 1); -- fail--
2.37.1
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 using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: martin.kalcher@aboutsource.net, pgsql-hackers@lists.postgresql.org
Subject: Re: [Patch] Fix bounds check in trim_array()
In-Reply-To: <baaeb413-b8a8-4656-5757-ef347e5ec11f@aboutsource.net>
* 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