SPIGOT-7676: Enforce locale parameter in toLowerCase and toUpperCase method calls and always use root locale

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2024-06-03 07:18:17 +10:00
parent 4b6edd3c82
commit d267f74a0b
10 changed files with 36 additions and 19 deletions

View file

@ -26,6 +26,16 @@
<property name="files" value=".*[/\\]net[/\\]minecraft[/\\].*"/> <property name="files" value=".*[/\\]net[/\\]minecraft[/\\].*"/>
</module> </module>
<!-- See SPIGOT-7676: Enforce Locale, to prevent issues with turkish 'I' and similar -->
<module name="RegexpSingleline">
<property name="format" value="\.toUpperCase\(\s*\)" />
<property name="message" value="Use toUpperCase(Locale.ROOT) instead of toUpperCase()" />
</module>
<module name="RegexpSingleline">
<property name="format" value="\.toLowerCase\(\s*\)" />
<property name="message" value="Use toLowerCase(Locale.ROOT) instead of toLowerCase()" />
</module>
<module name="TreeWalker"> <module name="TreeWalker">
<!-- See https://checkstyle.org/config_javadoc.html --> <!-- See https://checkstyle.org/config_javadoc.html -->
<module name="AtclauseOrder"/> <module name="AtclauseOrder"/>

View file

@ -188,7 +188,7 @@
+ dimension = -999; + dimension = -999;
+ } + }
+ +
+ String worldType = (dimension == -999) ? dimensionKey.location().getNamespace() + "_" + dimensionKey.location().getPath() : org.bukkit.World.Environment.getEnvironment(dimension).toString().toLowerCase(); + String worldType = (dimension == -999) ? dimensionKey.location().getNamespace() + "_" + dimensionKey.location().getPath() : org.bukkit.World.Environment.getEnvironment(dimension).toString().toLowerCase(Locale.ROOT);
+ String name = (dimensionKey == WorldDimension.OVERWORLD) ? s : s + "_" + worldType; + String name = (dimensionKey == WorldDimension.OVERWORLD) ? s : s + "_" + worldType;
+ if (dimension != 0) { + if (dimension != 0) {
+ File newWorld = Convertable.getStorageFolder(new File(name).toPath(), dimensionKey).toFile(); + File newWorld = Convertable.getStorageFolder(new File(name).toPath(), dimensionKey).toFile();

View file

@ -584,10 +584,10 @@ public final class CraftServer implements Server {
return found; return found;
} }
String lowerName = name.toLowerCase(java.util.Locale.ENGLISH); String lowerName = name.toLowerCase(Locale.ROOT);
int delta = Integer.MAX_VALUE; int delta = Integer.MAX_VALUE;
for (Player player : getOnlinePlayers()) { for (Player player : getOnlinePlayers()) {
if (player.getName().toLowerCase(java.util.Locale.ENGLISH).startsWith(lowerName)) { if (player.getName().toLowerCase(Locale.ROOT).startsWith(lowerName)) {
int curDelta = Math.abs(player.getName().length() - lowerName.length()); int curDelta = Math.abs(player.getName().length() - lowerName.length());
if (curDelta < delta) { if (curDelta < delta) {
found = player; found = player;
@ -641,7 +641,7 @@ public final class CraftServer implements Server {
matchedPlayers.add(iterPlayer); matchedPlayers.add(iterPlayer);
break; break;
} }
if (iterPlayerName.toLowerCase(java.util.Locale.ENGLISH).contains(partialName.toLowerCase(java.util.Locale.ENGLISH))) { if (iterPlayerName.toLowerCase(Locale.ROOT).contains(partialName.toLowerCase(Locale.ROOT))) {
// Partial match // Partial match
matchedPlayers.add(iterPlayer); matchedPlayers.add(iterPlayer);
} }
@ -1203,7 +1203,7 @@ public final class CraftServer implements Server {
} else if (name.equals(levelName + "_the_end")) { } else if (name.equals(levelName + "_the_end")) {
worldKey = net.minecraft.world.level.World.END; worldKey = net.minecraft.world.level.World.END;
} else { } else {
worldKey = ResourceKey.create(Registries.DIMENSION, new MinecraftKey(name.toLowerCase(java.util.Locale.ENGLISH))); worldKey = ResourceKey.create(Registries.DIMENSION, new MinecraftKey(name.toLowerCase(Locale.ROOT)));
} }
// If set to not keep spawn in memory (changed from default) then adjust rule accordingly // If set to not keep spawn in memory (changed from default) then adjust rule accordingly
@ -1213,7 +1213,7 @@ public final class CraftServer implements Server {
WorldServer internal = (WorldServer) new WorldServer(console, console.executor, worldSession, worlddata, worldKey, worlddimension, getServer().progressListenerFactory.create(worlddata.getGameRules().getInt(GameRules.RULE_SPAWN_CHUNK_RADIUS)), WorldServer internal = (WorldServer) new WorldServer(console, console.executor, worldSession, worlddata, worldKey, worlddimension, getServer().progressListenerFactory.create(worlddata.getGameRules().getInt(GameRules.RULE_SPAWN_CHUNK_RADIUS)),
worlddata.isDebugWorld(), j, creator.environment() == Environment.NORMAL ? list : ImmutableList.of(), true, console.overworld().getRandomSequences(), creator.environment(), generator, biomeProvider); worlddata.isDebugWorld(), j, creator.environment() == Environment.NORMAL ? list : ImmutableList.of(), true, console.overworld().getRandomSequences(), creator.environment(), generator, biomeProvider);
if (!(worlds.containsKey(name.toLowerCase(java.util.Locale.ENGLISH)))) { if (!(worlds.containsKey(name.toLowerCase(Locale.ROOT)))) {
return null; return null;
} }
@ -1273,7 +1273,7 @@ public final class CraftServer implements Server {
getLogger().log(Level.SEVERE, null, ex); getLogger().log(Level.SEVERE, null, ex);
} }
worlds.remove(world.getName().toLowerCase(java.util.Locale.ENGLISH)); worlds.remove(world.getName().toLowerCase(Locale.ROOT));
console.removeLevel(handle); console.removeLevel(handle);
return true; return true;
} }
@ -1286,7 +1286,7 @@ public final class CraftServer implements Server {
public World getWorld(String name) { public World getWorld(String name) {
Preconditions.checkArgument(name != null, "name cannot be null"); Preconditions.checkArgument(name != null, "name cannot be null");
return worlds.get(name.toLowerCase(java.util.Locale.ENGLISH)); return worlds.get(name.toLowerCase(Locale.ROOT));
} }
@Override @Override
@ -1305,7 +1305,7 @@ public final class CraftServer implements Server {
System.out.println("World " + world.getName() + " is a duplicate of another world and has been prevented from loading. Please delete the uid.dat file from " + world.getName() + "'s world directory if you want to be able to load the duplicate world."); System.out.println("World " + world.getName() + " is a duplicate of another world and has been prevented from loading. Please delete the uid.dat file from " + world.getName() + "'s world directory if you want to be able to load the duplicate world.");
return; return;
} }
worlds.put(world.getName().toLowerCase(java.util.Locale.ENGLISH), world); worlds.put(world.getName().toLowerCase(Locale.ROOT), world);
} }
@Override @Override

View file

@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableMap.Builder;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -80,7 +81,7 @@ public final class CraftChatMessage {
} }
switch (groupId) { switch (groupId) {
case 1: case 1:
char c = match.toLowerCase(java.util.Locale.ENGLISH).charAt(1); char c = match.toLowerCase(Locale.ROOT).charAt(1);
EnumChatFormat format = formatMap.get(c); EnumChatFormat format = formatMap.get(c);
if (c == 'x') { if (c == 'x') {

View file

@ -3,6 +3,7 @@ package org.bukkit;
import static org.bukkit.support.MatcherAssert.*; import static org.bukkit.support.MatcherAssert.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.util.Locale;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.MinecraftKey; import net.minecraft.resources.MinecraftKey;
import org.bukkit.craftbukkit.CraftSound; import org.bukkit.craftbukkit.CraftSound;
@ -21,7 +22,7 @@ public class SoundTest extends AbstractTestingBase {
@Test @Test
public void testReverse() { public void testReverse() {
for (MinecraftKey effect : BuiltInRegistries.SOUND_EVENT.keySet()) { for (MinecraftKey effect : BuiltInRegistries.SOUND_EVENT.keySet()) {
assertNotNull(Sound.valueOf(effect.getPath().replace('.', '_').toUpperCase(java.util.Locale.ENGLISH)), effect + ""); assertNotNull(Sound.valueOf(effect.getPath().replace('.', '_').toUpperCase(Locale.ROOT)), effect + "");
} }
} }

View file

@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.*;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -82,7 +83,7 @@ public class DeprecatedItemMetaCustomValueTest extends AbstractTestingBase {
} }
private NamespacedKey requestKey(String keyName) { private NamespacedKey requestKey(String keyName) {
return new NamespacedKey("test-plugin", keyName.toLowerCase()); return new NamespacedKey("test-plugin", keyName.toLowerCase(Locale.ROOT));
} }
/* /*

View file

@ -7,6 +7,7 @@ import java.lang.reflect.Array;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -96,7 +97,7 @@ public class PersistentDataContainerTest extends AbstractTestingBase {
} }
private static NamespacedKey requestKey(String keyName) { private static NamespacedKey requestKey(String keyName) {
return new NamespacedKey("test-plugin", keyName.toLowerCase()); return new NamespacedKey("test-plugin", keyName.toLowerCase(Locale.ROOT));
} }
@Test @Test

View file

@ -3,6 +3,7 @@ package org.bukkit.generator.structure;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Locale;
import net.minecraft.core.IRegistry; import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.resources.MinecraftKey; import net.minecraft.resources.MinecraftKey;
@ -25,7 +26,7 @@ public class StructureTest extends AbstractTestingBase {
} }
String name = field.getName(); String name = field.getName();
assertNotNull(Registry.STRUCTURE.get(NamespacedKey.fromString(name.toLowerCase())), "No structure for field name " + name); assertNotNull(Registry.STRUCTURE.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT))), "No structure for field name " + name);
} }
} }
@ -36,7 +37,7 @@ public class StructureTest extends AbstractTestingBase {
MinecraftKey minecraftKey = structureBuiltInRegistries.getKey(structure); MinecraftKey minecraftKey = structureBuiltInRegistries.getKey(structure);
try { try {
Structure bukkit = (Structure) Structure.class.getField(minecraftKey.getPath().toUpperCase()).get(null); Structure bukkit = (Structure) Structure.class.getField(minecraftKey.getPath().toUpperCase(Locale.ROOT)).get(null);
assertEquals(minecraftKey, CraftNamespacedKey.toMinecraft(bukkit.getKey()), "Keys are not the same for " + minecraftKey); assertEquals(minecraftKey, CraftNamespacedKey.toMinecraft(bukkit.getKey()), "Keys are not the same for " + minecraftKey);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {

View file

@ -3,6 +3,7 @@ package org.bukkit.generator.structure;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Locale;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.MinecraftKey; import net.minecraft.resources.MinecraftKey;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
@ -24,7 +25,7 @@ public class StructureTypeTest extends AbstractTestingBase {
} }
String name = field.getName(); String name = field.getName();
assertNotNull(Registry.STRUCTURE_TYPE.get(NamespacedKey.fromString(name.toLowerCase())), "No enchantment for field name " + name); assertNotNull(Registry.STRUCTURE_TYPE.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT))), "No enchantment for field name " + name);
} }
} }
@ -34,7 +35,7 @@ public class StructureTypeTest extends AbstractTestingBase {
MinecraftKey minecraftKey = BuiltInRegistries.STRUCTURE_TYPE.getKey(structureType); MinecraftKey minecraftKey = BuiltInRegistries.STRUCTURE_TYPE.getKey(structureType);
try { try {
StructureType bukkit = (StructureType) StructureType.class.getField(minecraftKey.getPath().toUpperCase()).get(null); StructureType bukkit = (StructureType) StructureType.class.getField(minecraftKey.getPath().toUpperCase(Locale.ROOT)).get(null);
assertEquals(minecraftKey, CraftNamespacedKey.toMinecraft(bukkit.getKey()), "Keys are not the same for " + minecraftKey); assertEquals(minecraftKey, CraftNamespacedKey.toMinecraft(bukkit.getKey()), "Keys are not the same for " + minecraftKey);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {

View file

@ -5,6 +5,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import net.minecraft.core.IRegistry; import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.resources.MinecraftKey; import net.minecraft.resources.MinecraftKey;
@ -48,7 +49,7 @@ public class RegistryConstantsTest extends AbstractTestingBase {
} }
String name = field.getName(); String name = field.getName();
NamespacedKey key = NamespacedKey.fromString(name.toLowerCase()); NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
if (registry.get(key) == null) { if (registry.get(key) == null) {
excessKeys.add(key); excessKeys.add(key);
} }
@ -67,7 +68,7 @@ public class RegistryConstantsTest extends AbstractTestingBase {
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T bukkitObject = (T) clazz.getField(minecraftKey.getPath().toUpperCase()).get(null); T bukkitObject = (T) clazz.getField(minecraftKey.getPath().toUpperCase(Locale.ROOT)).get(null);
assertEquals(minecraftKey, CraftNamespacedKey.toMinecraft(bukkitObject.getKey()), "Keys are not the same for " + minecraftKey); assertEquals(minecraftKey, CraftNamespacedKey.toMinecraft(bukkitObject.getKey()), "Keys are not the same for " + minecraftKey);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {