Skip to content

Commit 63a03ae

Browse files
committed
Fix list_copy_head() with empty Lists
list_copy_head() given an empty List would crash from trying to dereference the List to obtain its length. Since NIL is how we represent an empty List, we should just be returning another empty List in this case. list_copy_head() is new to v16, so let's fix it now before too many people start coding around the buggy NIL behavior. Reported-by: Miroslav Bendik Discussion: https://postgr.es/m/CAPoEpV02WhawuWnmnKet6BqU63bEu7oec0pJc=nKMtPsHMzTXQ@mail.gmail.com
1 parent 94d73f9 commit 63a03ae

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/backend/nodes/list.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,11 +1553,11 @@ list_copy_head(const List *oldlist, int len)
15531553
{
15541554
List *newlist;
15551555

1556-
len = Min(oldlist->length, len);
1557-
1558-
if (len <= 0)
1556+
if (oldlist == NIL || len <= 0)
15591557
return NIL;
15601558

1559+
len = Min(oldlist->length, len);
1560+
15611561
newlist = new_list(oldlist->type, len);
15621562
memcpy(newlist->elements, oldlist->elements, len * sizeof(ListCell));
15631563

0 commit comments

Comments
 (0)