8000 plperl: Correctly handle empty arrays in plperl_ref_from_pg_array. · prmdeveloper/postgres@44f9f1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 44f9f1f

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 b73e816 commit 44f9f1f

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
@@ -1433,17 +1433,25 @@ plperl_ref_from_pg_array(Datum arg, Oid typid)
14331433
info->ndims = ARR_NDIM(ar);
14341434
dims = ARR_DIMS(ar);
14351435

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

1440-
/* Get total number of elements in each dimension */
1441-
info->nelems = palloc(sizeof(int) * info->ndims);
1442-
info->nelems[0] = nitems;
1443-
for (i = 1; i < info->ndims; i++)
1444-
info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
1447+
/* Get total number of elements in each dimension */
1448+
info->nelems = palloc(sizeof(int) * info->ndims);
1449+
info->nelems[0] = nitems;
1450+
for (i = 1; i < info->ndims; i++)
1451+
info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
14451452

1446-
av = split_array(info, 0, nitems, 0);
1453+
av = split_array(info, 0, nitems, 0);
1454+
}
14471455

14481456
hv = newHV();
14491457
(void) hv_store(hv, "array", 5, av, 0);
@@ -1462,6 +1470,9 @@ split_array(plperl_array_info *info, int first, int last, int nest)
14621470
int i;
14631471
AV *result;
14641472

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

0 commit comments

Comments
 (0)
0