Commit e66197fa authored by Andres Freund's avatar Andres Freund

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-
parent 8c314b98
...@@ -1450,6 +1450,13 @@ plperl_ref_from_pg_array(Datum arg, Oid typid) ...@@ -1450,6 +1450,13 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
info->ndims = ARR_NDIM(ar); info->ndims = ARR_NDIM(ar);
dims = ARR_DIMS(ar); dims = ARR_DIMS(ar);
/* No dimensions? Return an empty array */
if (info->ndims == 0)
{
av = newRV_noinc((SV *) newAV());
}
else
{
deconstruct_array(ar, elementtype, typlen, typbyval, deconstruct_array(ar, elementtype, typlen, typbyval,
typalign, &info->elements, &info->nulls, typalign, &info->elements, &info->nulls,
&nitems); &nitems);
...@@ -1461,6 +1468,7 @@ plperl_ref_from_pg_array(Datum arg, Oid typid) ...@@ -1461,6 +1468,7 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
info->nelems[i] = info->nelems[i - 1] / dims[i - 1]; info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
av = split_array(info, 0, nitems, 0); av = split_array(info, 0, nitems, 0);
}
hv = newHV(); hv = newHV();
(void) hv_store(hv, "array", 5, av, 0); (void) hv_store(hv, "array", 5, av, 0);
...@@ -1479,6 +1487,9 @@ split_array(plperl_array_info *info, int first, int last, int nest) ...@@ -1479,6 +1487,9 @@ split_array(plperl_array_info *info, int first, int last, int nest)
int i; int i;
AV *result; AV *result;
/* we should only be called when we have something to split */
Assert(info->ndims > 0);
/* since this function recurses, it could be driven to stack overflow */ /* since this function recurses, it could be driven to stack overflow */
check_stack_depth(); check_stack_depth();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment