Mojira Archive
MC-221421

A list tag can be modified during insertion into itself

The bug

While all elements of a list tag are being inserted into all elements of the same list tag, the source list tag is modified.

How to reproduce

  1. data modify storage mc-221421: list set value [[], []]
  2. data modify storage mc-221421: list[] append from storage mc-221421: list[]
  3. data get storage mc-221421: list

Expected behavior

Storage mc-221421: has the following contents: [[[], []], [[], []]]
  1. The tags [], [] are inserted into each tag.

Actual behavior

Storage mc-221421: has the following contents: [[[], []], [[[], []], [[[], []]]]]
  1. The tags [], [] are inserted into the first tag.
  2. The modified first tag is inserted into the second tag.
  3. The modified second tag is inserted into the second tag.

Code analysis

net.minecraft.server.commands.data.DataCommands.java
private static int insertAtIndex(int index, CompoundTag tag, NbtPath path, List<Tag> sources) throws CommandSyntaxException {
    var targets = path.getOrCreate(tag, ListTag::new);
    var result = 0;

    for (var target : targets) {
        if (!(target instanceof CollectionTag)) {
            throw ERROR_EXPECTED_LIST.create(target);
        }

        var success = false;
        var collection = (CollectionTag<?>) target;
        var cursor = index < 0 ? collection.size() + index + 1 : index;

        for (var source : sources) {
            try {
                if (collection.addTag(cursor, source.copy() /* Copying should be done in batches outside of the sources iteration. */)) {
                    ++cursor;
                    success = true;
                }
            } catch (IndexOutOfBoundsException e) {
                throw ERROR_INVALID_INDEX.create(cursor);
            }
        }

        result += success ? 1 : 0;
    }

    return result;
}

Fixed

intsuc

2021-03-30, 05:19 PM

2022-12-02, 10:43 PM

2022-12-02, 10:43 PM

6

3

Confirmed

Commands

nbt

1.16.5 - 1.19.21.16.5, 21w11a, 21w13a, 21w15a, 21w16a, 21w19a, 1.17 Pre-release 1, 1.17, 1.17.1, 21w37a, 1.18.1, 22w03a, 22w05a, 1.18.2 Pre-release 1, 1.18.2, 22w11a, 22w12a, 1.19 Release Candidate 2, 1.19, 1.19.1, 1.19.2

1.19.3 Pre-release 3