[poincare] Escape replace/removeChild when tree is alloc fail

This commit is contained in:
Léa Saviot
2018-08-20 17:01:44 +02:00
parent 35cbdeabf5
commit cd8b460001
2 changed files with 20 additions and 6 deletions

View File

@@ -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

View File

@@ -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());
}