diff --git a/src/main/java/com/amazon/ion/IonContainer.java b/src/main/java/com/amazon/ion/IonContainer.java index 12805d8ea..db75ee37a 100644 --- a/src/main/java/com/amazon/ion/IonContainer.java +++ b/src/main/java/com/amazon/ion/IonContainer.java @@ -22,6 +22,8 @@ * Common functionality of Ion struct, list, and * sexp types. *

+ * A container cannot be inserted into itself or one of its descendants. + *

* WARNING: This interface should not be implemented or extended by * code outside of this library. */ diff --git a/src/main/java/com/amazon/ion/impl/lite/IonContainerLite.java b/src/main/java/com/amazon/ion/impl/lite/IonContainerLite.java index f51376d9b..6bef18f54 100644 --- a/src/main/java/com/amazon/ion/impl/lite/IonContainerLite.java +++ b/src/main/java/com/amazon/ion/impl/lite/IonContainerLite.java @@ -601,7 +601,7 @@ public boolean add(IonValue child) /** * Ensures that a potential new child is non-null, has no container, - * is not read-only, and is not a datagram. + * is not read-only, and can be inserted without creating a cycle. * * @throws NullPointerException * if {@code child} is {@code null}. @@ -610,7 +610,7 @@ public boolean add(IonValue child) * @throws ReadOnlyValueException * if {@code child} is read only. * @throws IllegalArgumentException - * if {@code child} is an {@link IonDatagram}. + * if {@code child} is a datagram, this container, or one of its ancestors. */ void validateNewChild(IonValue child) throws ContainedValueException, NullPointerException, @@ -630,6 +630,14 @@ void validateNewChild(IonValue child) throw new IllegalArgumentException(message); } + // A detached ancestor must be the root. Skip the walk for scalars and empty children. + if (child == this || (child instanceof IonContainerLite + && ((IonContainerLite) child)._child_count != 0 + && !(this instanceof IonDatagram) && topLevelValue() == child)) + { + throw new IllegalArgumentException("A container cannot contain itself or an ancestor."); + } + assert child instanceof IonValueLite : "Child was not created by the same ValueFactory"; diff --git a/src/test/java/com/amazon/ion/ListTest.java b/src/test/java/com/amazon/ion/ListTest.java index 43c74fa40..da2c930d2 100644 --- a/src/test/java/com/amazon/ion/ListTest.java +++ b/src/test/java/com/amazon/ion/ListTest.java @@ -83,6 +83,34 @@ protected IonList wrap(IonValue... children) //========================================================================= // Test cases + @Test + public void testCannotAddContainerToItself() + { + IonList list = system().newEmptyList(); + assertThrows(IllegalArgumentException.class, () -> list.add(list)); + assertEquals(0, list.size()); + assertNull(list.getContainer()); + } + + @Test + public void testCannotReplaceChildWithAncestor() + { + IonList root = system().newEmptyList(); + IonSexp sequence = root.add().newEmptySexp(); + IonStruct leaf = sequence.add().newEmptyStruct(); + IonInt original = leaf.add("value").newInt(1); + + assertThrows(IllegalArgumentException.class, () -> sequence.set(0, root)); + assertThrows(IllegalArgumentException.class, () -> leaf.put("value", root)); + assertSame(leaf, sequence.get(0)); + assertSame(original, leaf.get("value")); + assertNull(root.getContainer()); + + leaf.removeFromContainer(); + leaf.put("value", root); + assertSame(leaf, root.getContainer()); + } + @Test public void testFactoryNullList() {