mojira.dev
MC-163943

Read-only scores can be mutated by swapping

The bug

/scoreboard players <targets> <targetObjective> >< <source> <sourceObjective> can mutate source scores even though <sourceObjective> is read-only.

How to reproduce

  1. /scoreboard objectives add writable dummy
  2. /scoreboard objectives add readonly air
  3. /scoreboard players set value writable 42
  4. /scoreboard players operation value writable >< value readonly
  5. /scoreboard players get value readonly

    value has 42 [readonly], the score value in the objective readonly is mutated.

Code analysis

  • net.minecraft.server.commands.ScoreboardCommand

    performOperation(
        commandContext.getSource(),
        ScoreHolderArgument.getNamesWithDefaultWildcard(commandContext, "targets"),
        ObjectiveArgument.getWritableObjective(commandContext, "targetObjective"),
        OperationArgument.getOperation(commandContext, "operation"),
        ScoreHolderArgument.getNamesWithDefaultWildcard(commandContext, "source"),
        ObjectiveArgument.getObjective(commandContext, "sourceObjective") // accepts all objectives here (including read-only)
    );
  • net.minecraft.commands.arguments.OperationArgument

    private static Operation getOperation(String string) throws CommandSyntaxException {
        return string.equals("><") ? (score1, score2) -> {
            int integer3 = score1.getScore();
            score1.setScore(score2.getScore());
            score2.setScore(integer3); // mutated here
        } : getSimpleOperation(string);
    }

Comments 3

Can't reproduce, I get the error message as expected. Even doing the tellraw at the same tick with a chain command block reveals, that the score was not mutated in the tick.

Sorry, nvm, I had the objectives the wrong way around

Fixed in 23w46a. Now the following error message is shown in step 4 and the readonly score is not mutated.

An unexpected error occurred trying to execute that command: Cannot modify read-only score

intsuc

(Unassigned)

Confirmed

Commands

scoreboard

1.14.4, 19w42a, 19w44a, 1.15 Pre-release 5, 1.15.2, ..., 23w18a, 1.20.1, 23w31a, 1.20.2, 23w42a

23w46a

Retrieved