Skip to content

Commit 6041d38

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 2afe954 commit 6041d38

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/pl/plperl/plperl.c

+20-9
Original file line numberDiff line numberDiff line change
@@ -1431,17 +1431,25 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
14311431
info->ndims = ARR_NDIM(ar);
14321432
dims = ARR_DIMS(ar);
14331433

1434-
deconstruct_array(ar, elementtype, typlen, typbyval,
1435-
typalign, &info->elements, &info->nulls,
1436-
&nitems);
1434+
/* No dimensions? Return an empty array */
1435+
if (info->ndims == 0)
1436+
{
1437+
av = newRV_noinc((SV *) newAV());
1438+
}
1439+
else
1440+
{
1441+
deconstruct_array(ar, elementtype, typlen, typbyval,
1442+
typalign, &info->elements, &info->nulls,
1443+
&nitems);
14371444

1438-
/* Get total number of elements in each dimension */
1439-
info->nelems = palloc(sizeof(int) * info->ndims);
1440-
info->nelems[0] = nitems;
1441-
for (i = 1; i < info->ndims; i++)
1442-
info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
1445+
/* Get total number of elements in each dimension */
1446+
info->nelems = palloc(sizeof(int) * info->ndims);
1447+
info->nelems[0] = nitems;
1448+
for (i = 1; i < info->ndims; i++)
1449+
info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
14431450

1444-
av = split_array(info, 0, nitems, 0);
1451+
av = split_array(info, 0, nitems, 0);
1452+
}
14451453

14461454
hv = newHV();
14471455
(void) hv_store(hv, "array", 5, av, 0);
@@ -1460,6 +1468,9 @@ split_array(plperl_array_info *info, int first, int last, int nest)
14601468
int i;
14611469
AV *result;
14621470

1471+
/* we should only be called when we have something to split */
1472+
Assert(info->ndims > 0);
1473+
14631474
/* since this function recurses, it could be driven to stack overflow */
14641475
check_stack_depth();
14651476

0 commit comments

Comments
 (0)