Skip to content

Commit

Permalink
SPIGOT-7676: Enforce locale parameter in toLowerCase and toUpperCase …
Browse files Browse the repository at this point in the history
…method calls and always use root locale
  • Loading branch information
DerFrZocker authored and md-5 committed Jun 2, 2024
1 parent bdb40c5 commit 9003845
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 19 deletions.
10 changes: 10 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
<property name="files" value=".*[/\\]net[/\\]minecraft[/\\].*"/>
</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">
<!-- See https://checkstyle.org/config_javadoc.html -->
<module name="AtclauseOrder"/>
Expand Down
2 changes: 1 addition & 1 deletion nms-patches/net/minecraft/server/MinecraftServer.patch
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
+ 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;
+ if (dimension != 0) {
+ File newWorld = Convertable.getStorageFolder(new File(name).toPath(), dimensionKey).toFile();
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/bukkit/craftbukkit/CraftServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,10 +584,10 @@ public Player getPlayer(final String name) {
return found;
}

String lowerName = name.toLowerCase(java.util.Locale.ENGLISH);
String lowerName = name.toLowerCase(Locale.ROOT);
int delta = Integer.MAX_VALUE;
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());
if (curDelta < delta) {
found = player;
Expand Down Expand Up @@ -641,7 +641,7 @@ public List<Player> matchPlayer(String partialName) {
matchedPlayers.add(iterPlayer);
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
matchedPlayers.add(iterPlayer);
}
Expand Down Expand Up @@ -1203,7 +1203,7 @@ public World createWorld(WorldCreator creator) {
} else if (name.equals(levelName + "_the_end")) {
worldKey = net.minecraft.world.level.World.END;
} 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
Expand All @@ -1213,7 +1213,7 @@ public World createWorld(WorldCreator creator) {
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);

if (!(worlds.containsKey(name.toLowerCase(java.util.Locale.ENGLISH)))) {
if (!(worlds.containsKey(name.toLowerCase(Locale.ROOT)))) {
return null;
}

Expand Down Expand Up @@ -1273,7 +1273,7 @@ public boolean unloadWorld(World world, boolean save) {
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);
return true;
}
Expand All @@ -1286,7 +1286,7 @@ public DedicatedServer getServer() {
public World getWorld(String name) {
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
Expand All @@ -1305,7 +1305,7 @@ public void addWorld(World 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;
}
worlds.put(world.getName().toLowerCase(java.util.Locale.ENGLISH), world);
worlds.put(world.getName().toLowerCase(Locale.ROOT), world);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -80,7 +81,7 @@ private StringMessage(String message, boolean keepNewlines, boolean plain) {
}
switch (groupId) {
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);

if (c == 'x') {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/bukkit/SoundTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.bukkit.support.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Locale;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.MinecraftKey;
import org.bukkit.craftbukkit.CraftSound;
Expand All @@ -21,7 +22,7 @@ public void testGetSound() {
@Test
public void testReverse() {
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 + "");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.StringReader;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -82,7 +83,7 @@ private ItemMeta createNewItemMeta() {
}

private NamespacedKey requestKey(String keyName) {
return new NamespacedKey("test-plugin", keyName.toLowerCase());
return new NamespacedKey("test-plugin", keyName.toLowerCase(Locale.ROOT));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -96,7 +97,7 @@ private static ItemMeta createNewItemMeta() {
}

private static NamespacedKey requestKey(String keyName) {
return new NamespacedKey("test-plugin", keyName.toLowerCase());
return new NamespacedKey("test-plugin", keyName.toLowerCase(Locale.ROOT));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Locale;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.MinecraftKey;
Expand All @@ -25,7 +26,7 @@ public void testBukkitToMinecraftFieldName() {
}

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);
}
}

Expand All @@ -36,7 +37,7 @@ public void testMinecraftToBukkitFieldName() {
MinecraftKey minecraftKey = structureBuiltInRegistries.getKey(structure);

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);
} catch (NoSuchFieldException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Locale;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.MinecraftKey;
import org.bukkit.NamespacedKey;
Expand All @@ -24,7 +25,7 @@ public void testBukkitToMinecraftFieldName() {
}

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);
}
}

Expand All @@ -34,7 +35,7 @@ public void testMinecraftToBukkitFieldName() {
MinecraftKey minecraftKey = BuiltInRegistries.STRUCTURE_TYPE.getKey(structureType);

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);
} catch (NoSuchFieldException e) {
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/bukkit/registry/RegistryConstantsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.MinecraftKey;
Expand Down Expand Up @@ -48,7 +49,7 @@ private <T extends Keyed> void testExcessConstants(Class<T> clazz, Registry<T> r
}

String name = field.getName();
NamespacedKey key = NamespacedKey.fromString(name.toLowerCase());
NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
if (registry.get(key) == null) {
excessKeys.add(key);
}
Expand All @@ -67,7 +68,7 @@ private <T extends Keyed, M> void testMissingConstants(Class<T> clazz, ResourceK

try {
@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);
} catch (NoSuchFieldException e) {
Expand Down

0 comments on commit 9003845

Please sign in to comment.