Skip to content

Commit e66197f

Browse files
committed
plperl: Correctly handle empty arrays in plperl_ref_from_pg_array.
plperl_ref_from_pg_array() didn't consider the case that postgrs arrays can have 0 dimensions (when they're empty) and accessed the first dimension without a check. Fix that by special casing the empty array case. Author: Alex Hunsaker Reported-By: Andres Freund / valgrind / buildfarm animal skink Discussion: 20160308063240.usnzg6bsbjrne667@alap3.anarazel.de Backpatch: 9.1-
1 parent 8c314b9 commit e66197f

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/pl/plperl/plperl.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,17 +1450,25 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
14501450
info->ndims = ARR_NDIM(ar);
14511451
dims = ARR_DIMS(ar);
14521452

1453-
deconstruct_array(ar, elementtype, typlen, typbyval,
1454-
typalign, &info->elements, &info->nulls,
1455-
&nitems);
1453+
/* No dimensions? Return an empty array */
1454+
if (info->ndims == 0)
1455+
{
1456+
av = newRV_noinc((SV *) newAV());
1457+
}
1458+
else
1459+
{
1460+
deconstruct_array(ar, elementtype, typlen, typbyval,
1461+
typalign, &info->elements, &info->nulls,
1462+
&nitems);
14561463

1457-
/* Get total number of elements in each dimension */
1458-
info->nelems = palloc(sizeof(int) * info->ndims);
1459-
info->nelems[0] = nitems;
1460-
for (i = 1; i < info->ndims; i++)
1461-
info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
1464+
/* Get total number of elements in each dimension */
1465+
info->nelems = palloc(sizeof(int) * info->ndims);
1466+
info->nelems[0] = nitems;
1467+
for (i = 1; i < info->ndims; i++)
1468+
info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
14621469

1463-
av = split_array(info, 0, nitems, 0);
1470+
av = split_array(info, 0, nitems, 0);
1471+
}
14641472

14651473
hv = newHV();
14661474
(void) hv_store(hv, "array", 5, av, 0);
@@ -1479,6 +1487,9 @@ split_array(plperl_array_info *info, int first, int last, int nest)
14791487
int i;
14801488
AV *result;
14811489

1490+
/* we should only be called when we have something to split */
1491+
Assert(info->ndims > 0);
1492+
14821493
/* since this function recurses, it could be driven to stack overflow */
14831494
check_stack_depth();
14841495

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy