Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2026-06-28 Todd White <todd.white@thalion.global>
* Source/CFTree.c (CFTreePrependChild): Set _lastChild to the new
child when the tree was empty, instead of NULL.
(CFTreeGetChildAtIndex): Stop walking at the end of the child list
so an out-of-range index returns NULL instead of dereferencing it.
* Tests/CFTree/basic.m: Regression tests.

2021-09-30 Frederik Seiffert <frederik@algoriddim.com>
* Source/CFDate.c: Fix logic in CFGregorianDateIsValid()

Expand Down
6 changes: 3 additions & 3 deletions Source/CFTree.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ CFTreePrependChild (CFTreeRef tree, CFTreeRef newChild)
newChild->_nextSibling = tree->_firstChild;
tree->_firstChild = newChild;
if (tree->_lastChild == NULL)
tree->_lastChild = NULL;
tree->_lastChild = newChild;
}

void
Expand Down Expand Up @@ -240,9 +240,9 @@ CFTreeGetChildAtIndex (CFTreeRef tree, CFIndex idx)

j = 0;
child = tree->_firstChild;
while (j++ < idx)
while (child != NULL && j++ < idx)
child = child->_nextSibling;

return child;
}

Expand Down
23 changes: 22 additions & 1 deletion Tests/CFTree/basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,28 @@ int main (void)
PASS_CF(CFTreeGetNextSibling (child1) == child2,
"Next sibling for child1 is child2.");
PASS_CF(CFTreeGetChildAtIndex (tree, 2) == child3, "Child3 is at index 2");


{
/* Prepending to an empty tree must set _lastChild, otherwise the next
append dereferences NULL. An out-of-range index must return NULL
rather than walking off the end of the child list. */
CFTreeRef t2 = CFTreeCreate (NULL, &ctxt);
CFTreeRef a = CFTreeCreate (NULL, &ctxt);
CFTreeRef b = CFTreeCreate (NULL, &ctxt);

CFTreePrependChild (t2, a);
CFTreeAppendChild (t2, b);
PASS_CF(CFTreeGetChildCount (t2) == 2
&& CFTreeGetChildAtIndex (t2, 1) == b,
"Append after prepend-to-empty works.");
PASS_CF(CFTreeGetChildAtIndex (t2, 5) == NULL,
"Out-of-range child index returns NULL.");

CFRelease (a);
CFRelease (b);
CFRelease (t2);
}

CFRelease (child1);
CFRelease (child2);
CFRelease (child3);
Expand Down
Loading