Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/main/java/com/amazon/ion/IonContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* Common functionality of Ion <code>struct</code>, <code>list</code>, and
* <code>sexp</code> types.
* <p>
* A container cannot be inserted into itself or one of its descendants.
* <p>
* <b>WARNING:</b> This interface should not be implemented or extended by
* code outside of this library.
*/
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/amazon/ion/impl/lite/IonContainerLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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,
Expand All @@ -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";

Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/amazon/ion/ListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Loading