diff --git a/poincare/include/poincare/tree_by_reference.h b/poincare/include/poincare/tree_by_reference.h index 9060c54b0..0b785116c 100644 --- a/poincare/include/poincare/tree_by_reference.h +++ b/poincare/include/poincare/tree_by_reference.h @@ -83,11 +83,7 @@ public: // Replace void replaceWithInPlace(TreeByReference t); void replaceChildInPlace(TreeByReference oldChild, TreeByReference newChild); - void replaceChildAtIndexInPlace(int oldChildIndex, TreeByReference newChild) { - assert(oldChildIndex >= 0 && oldChildIndex < numberOfChildren()); - TreeByReference oldChild = childAtIndex(oldChildIndex); - replaceChildInPlace(oldChild, newChild); - } + void replaceChildAtIndexInPlace(int oldChildIndex, TreeByReference newChild); void replaceWithAllocationFailureInPlace(int currentNumberOfChildren); void replaceChildWithGhostInPlace(TreeByReference t); // Merge diff --git a/poincare/src/tree_by_reference.cpp b/poincare/src/tree_by_reference.cpp index 45a3ad043..378c6fae0 100644 --- a/poincare/src/tree_by_reference.cpp +++ b/poincare/src/tree_by_reference.cpp @@ -86,6 +86,18 @@ void TreeByReference::replaceChildInPlace(TreeByReference oldChild, TreeByRefere oldChild.node()->release(oldChild.numberOfChildren()); } +void TreeByReference::replaceChildAtIndexInPlace(int oldChildIndex, TreeByReference newChild) { + if (oldChildIndex < 0 || oldChildIndex >= numberOfChildren()) { + /* The only case where the index might be out of range is when a tree has + * become an allocation failure, in which case we need to escape the invalid + * child removal. */ + assert(isAllocationFailure()); + return; + } + TreeByReference oldChild = childAtIndex(oldChildIndex); + replaceChildInPlace(oldChild, newChild); +} + void TreeByReference::replaceWithAllocationFailureInPlace(int currentNumberOfChildren) { if (isAllocationFailure()) { return; @@ -219,7 +231,13 @@ void TreeByReference::addChildAtIndexInPlace(TreeByReference t, int index, int c void TreeByReference::removeChildAtIndexInPlace(int i) { assert(!isUninitialized()); - assert(i >= 0 && i < numberOfChildren()); + if (i < 0 || i >= numberOfChildren()) { + /* The only case where the index might be out of range is when a tree has + * become an allocation failure, in which case we need to escape the invalid + * child removal. */ + assert(isAllocationFailure()); + return; + } TreeByReference t = childAtIndex(i); removeChildInPlace(t, t.numberOfChildren()); }