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:
Bukkit/Spigot 2024-06-03 07:18:14 +10:00
parent b0df4fc813
commit a3fb15bc23
23 changed files with 76 additions and 45 deletions

View file

@ -32,6 +32,16 @@
<property name="files" value=".*[\\/]src[\\/]test[\\/].*\.java$"/>
</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"/>

View file

@ -3,6 +3,7 @@ package org.bukkit;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.Locale;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -50,7 +51,7 @@ public enum Art implements Keyed {
this.id = id;
this.width = width;
this.height = height;
this.key = NamespacedKey.minecraft(name().toLowerCase(java.util.Locale.ENGLISH));
this.key = NamespacedKey.minecraft(name().toLowerCase(Locale.ROOT));
}
/**
@ -113,13 +114,13 @@ public enum Art implements Keyed {
public static Art getByName(@NotNull String name) {
Preconditions.checkArgument(name != null, "Name cannot be null");
return BY_NAME.get(name.toLowerCase(java.util.Locale.ENGLISH));
return BY_NAME.get(name.toLowerCase(Locale.ROOT));
}
static {
for (Art art : values()) {
BY_ID.put(art.id, art);
BY_NAME.put(art.toString().toLowerCase(java.util.Locale.ENGLISH), art);
BY_NAME.put(art.toString().toLowerCase(Locale.ROOT), art);
}
}
}

View file

@ -3,6 +3,7 @@ package org.bukkit;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
@ -428,6 +429,6 @@ public final class Color implements ConfigurationSerializable {
@Override
public String toString() {
return "Color:[argb0x" + Integer.toHexString(asARGB()).toUpperCase() + "]";
return "Color:[argb0x" + Integer.toHexString(asARGB()).toUpperCase(Locale.ROOT) + "]";
}
}

View file

@ -4954,7 +4954,7 @@ public enum Material implements Keyed, Translatable {
filtered = filtered.substring((NamespacedKey.MINECRAFT + ":").length());
}
filtered = filtered.toUpperCase(java.util.Locale.ENGLISH);
filtered = filtered.toUpperCase(Locale.ROOT);
filtered = filtered.replaceAll("\\s+", "_").replaceAll("\\W", "");
return getMaterial(filtered, legacyName);

View file

@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
@ -333,7 +334,7 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
default T match(@NotNull String input) {
Preconditions.checkArgument(input != null, "input must not be null");
String filtered = input.toLowerCase().replaceAll("\\s+", "_");
String filtered = input.toLowerCase(Locale.ROOT).replaceAll("\\s+", "_");
NamespacedKey namespacedKey = NamespacedKey.fromString(filtered);
return (namespacedKey != null) ? get(namespacedKey) : null;
}

View file

@ -5,6 +5,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Locale;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -86,7 +87,7 @@ public @interface Warning {
if (value == null) {
return DEFAULT;
}
WarningState state = values.get(value.toLowerCase());
WarningState state = values.get(value.toLowerCase(Locale.ROOT));
if (state == null) {
return DEFAULT;
}

View file

@ -1,6 +1,7 @@
package org.bukkit;
import com.google.common.collect.Maps;
import java.util.Locale;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -39,7 +40,7 @@ public enum WorldType {
*/
@Nullable
public static WorldType getByName(@NotNull String name) {
return BY_NAME.get(name.toUpperCase(java.util.Locale.ENGLISH));
return BY_NAME.get(name.toUpperCase(Locale.ROOT));
}
static {

View file

@ -2,6 +2,7 @@ package org.bukkit.attribute;
import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
@ -159,11 +160,11 @@ public class AttributeModifier implements ConfigurationSerializable {
@NotNull
public static AttributeModifier deserialize(@NotNull Map<String, Object> args) {
if (args.containsKey("slot")) {
EquipmentSlotGroup slotGroup = EquipmentSlotGroup.getByName(args.get("slot").toString().toLowerCase());
EquipmentSlotGroup slotGroup = EquipmentSlotGroup.getByName(args.get("slot").toString().toLowerCase(Locale.ROOT));
if (slotGroup == null) {
slotGroup = EquipmentSlotGroup.ANY;
EquipmentSlot slot = EquipmentSlot.valueOf((args.get("slot").toString().toUpperCase()));
EquipmentSlot slot = EquipmentSlot.valueOf((args.get("slot").toString().toUpperCase(Locale.ROOT)));
if (slot != null) {
slotGroup = slot.getGroup();
}

View file

@ -8,6 +8,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.Server;
@ -67,8 +68,8 @@ public class SimpleCommandMap implements CommandMap {
*/
@Override
public boolean register(@NotNull String label, @NotNull String fallbackPrefix, @NotNull Command command) {
label = label.toLowerCase(java.util.Locale.ENGLISH).trim();
fallbackPrefix = fallbackPrefix.toLowerCase(java.util.Locale.ENGLISH).trim();
label = label.toLowerCase(Locale.ROOT).trim();
fallbackPrefix = fallbackPrefix.toLowerCase(Locale.ROOT).trim();
boolean registered = register(label, command, false, fallbackPrefix);
Iterator<String> iterator = command.getAliases().iterator();
@ -136,7 +137,7 @@ public class SimpleCommandMap implements CommandMap {
return false;
}
String sentCommandLabel = args[0].toLowerCase(java.util.Locale.ENGLISH);
String sentCommandLabel = args[0].toLowerCase(Locale.ROOT);
Command target = getCommand(sentCommandLabel);
if (target == null) {
@ -168,7 +169,7 @@ public class SimpleCommandMap implements CommandMap {
@Override
@Nullable
public Command getCommand(@NotNull String name) {
Command target = knownCommands.get(name.toLowerCase(java.util.Locale.ENGLISH));
Command target = knownCommands.get(name.toLowerCase(Locale.ROOT));
return target;
}
@ -272,9 +273,9 @@ public class SimpleCommandMap implements CommandMap {
// We register these as commands so they have absolute priority.
if (targets.size() > 0) {
knownCommands.put(alias.toLowerCase(java.util.Locale.ENGLISH), new FormattedCommandAlias(alias.toLowerCase(java.util.Locale.ENGLISH), targets.toArray(new String[targets.size()])));
knownCommands.put(alias.toLowerCase(Locale.ROOT), new FormattedCommandAlias(alias.toLowerCase(Locale.ROOT), targets.toArray(new String[targets.size()])));
} else {
knownCommands.remove(alias.toLowerCase(java.util.Locale.ENGLISH));
knownCommands.remove(alias.toLowerCase(Locale.ROOT));
}
}
}

View file

@ -15,6 +15,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import org.bukkit.Bukkit;
@ -61,9 +62,9 @@ public class VersionCommand extends BukkitCommand {
}
boolean found = false;
pluginName = pluginName.toLowerCase(java.util.Locale.ENGLISH);
pluginName = pluginName.toLowerCase(Locale.ROOT);
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.getName().toLowerCase(java.util.Locale.ENGLISH).contains(pluginName)) {
if (plugin.getName().toLowerCase(Locale.ROOT).contains(pluginName)) {
describeToSender(plugin, sender);
found = true;
}
@ -133,7 +134,7 @@ public class VersionCommand extends BukkitCommand {
if (args.length == 1) {
List<String> completions = new ArrayList<String>();
String toComplete = args[0].toLowerCase(java.util.Locale.ENGLISH);
String toComplete = args[0].toLowerCase(Locale.ROOT);
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (StringUtil.startsWithIgnoreCase(plugin.getName(), toComplete)) {
completions.add(plugin.getName());

View file

@ -2,6 +2,7 @@ package org.bukkit.enchantments;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.Locale;
import org.bukkit.Keyed;
import org.bukkit.MinecraftExperimental;
import org.bukkit.MinecraftExperimental.Requires;
@ -351,7 +352,7 @@ public abstract class Enchantment implements Keyed, Translatable {
return null;
}
return getByKey(NamespacedKey.fromString(name.toLowerCase()));
return getByKey(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
}
/**

View file

@ -2,6 +2,7 @@ package org.bukkit.entity;
import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
@ -331,7 +332,7 @@ public enum EntityType implements Keyed, Translatable {
static {
for (EntityType type : values()) {
if (type.name != null) {
NAME_MAP.put(type.name.toLowerCase(java.util.Locale.ENGLISH), type);
NAME_MAP.put(type.name.toLowerCase(Locale.ROOT), type);
}
if (type.typeId > 0) {
ID_MAP.put(type.typeId, type);
@ -402,7 +403,7 @@ public enum EntityType implements Keyed, Translatable {
if (name == null) {
return null;
}
return NAME_MAP.get(name.toLowerCase(java.util.Locale.ENGLISH));
return NAME_MAP.get(name.toLowerCase(Locale.ROOT));
}
/**

View file

@ -2,6 +2,7 @@ package org.bukkit.inventory;
import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
import org.jetbrains.annotations.ApiStatus;
@ -73,7 +74,7 @@ public final class EquipmentSlotGroup implements Predicate<EquipmentSlot> {
public static EquipmentSlotGroup getByName(@NotNull String name) {
Preconditions.checkArgument(name != null, "Name cannot be null");
return BY_NAME.get(name.toLowerCase(java.util.Locale.ENGLISH));
return BY_NAME.get(name.toLowerCase(Locale.ROOT));
}
private static EquipmentSlotGroup get(@NotNull String key, @NotNull EquipmentSlot slot) {

View file

@ -4,6 +4,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
@ -52,7 +53,7 @@ public class PermissibleBase implements Permissible {
throw new IllegalArgumentException("Permission name cannot be null");
}
return permissions.containsKey(name.toLowerCase(java.util.Locale.ENGLISH));
return permissions.containsKey(name.toLowerCase(Locale.ROOT));
}
@Override
@ -70,7 +71,7 @@ public class PermissibleBase implements Permissible {
throw new IllegalArgumentException("Permission name cannot be null");
}
String name = inName.toLowerCase(java.util.Locale.ENGLISH);
String name = inName.toLowerCase(Locale.ROOT);
if (isPermissionSet(name)) {
return permissions.get(name).getValue();
@ -91,7 +92,7 @@ public class PermissibleBase implements Permissible {
throw new IllegalArgumentException("Permission cannot be null");
}
String name = perm.getName().toLowerCase(java.util.Locale.ENGLISH);
String name = perm.getName().toLowerCase(Locale.ROOT);
if (isPermissionSet(name)) {
return permissions.get(name).getValue();
@ -161,7 +162,7 @@ public class PermissibleBase implements Permissible {
Bukkit.getServer().getPluginManager().subscribeToDefaultPerms(isOp(), parent);
for (Permission perm : defaults) {
String name = perm.getName().toLowerCase(java.util.Locale.ENGLISH);
String name = perm.getName().toLowerCase(Locale.ROOT);
permissions.put(name, new PermissionAttachmentInfo(parent, name, null, true));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, parent);
calculateChildPermissions(perm.getChildren(), false, null);
@ -191,7 +192,7 @@ public class PermissibleBase implements Permissible {
Permission perm = Bukkit.getServer().getPluginManager().getPermission(name);
boolean value = entry.getValue() ^ invert;
String lname = name.toLowerCase(java.util.Locale.ENGLISH);
String lname = name.toLowerCase(Locale.ROOT);
permissions.put(lname, new PermissionAttachmentInfo(parent, lname, attachment, value));
Bukkit.getServer().getPluginManager().subscribeToPermission(name, parent);

View file

@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
@ -186,7 +187,7 @@ public class Permission {
@NotNull
public Permission addParent(@NotNull String name, boolean value) {
PluginManager pm = Bukkit.getServer().getPluginManager();
String lname = name.toLowerCase(java.util.Locale.ENGLISH);
String lname = name.toLowerCase(Locale.ROOT);
Permission perm = pm.getPermission(lname);

View file

@ -1,6 +1,7 @@
package org.bukkit.permissions;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
@ -89,7 +90,7 @@ public class PermissionAttachment {
* @param value New value of the permission
*/
public void setPermission(@NotNull String name, boolean value) {
permissions.put(name.toLowerCase(java.util.Locale.ENGLISH), value);
permissions.put(name.toLowerCase(Locale.ROOT), value);
permissible.recalculatePermissions();
}
@ -112,7 +113,7 @@ public class PermissionAttachment {
* @param name Name of the permission to remove
*/
public void unsetPermission(@NotNull String name) {
permissions.remove(name.toLowerCase(java.util.Locale.ENGLISH));
permissions.remove(name.toLowerCase(Locale.ROOT));
permissible.recalculatePermissions();
}

View file

@ -1,6 +1,7 @@
package org.bukkit.permissions;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -51,7 +52,7 @@ public enum PermissionDefault {
*/
@Nullable
public static PermissionDefault getByName(@NotNull String name) {
return lookup.get(name.toLowerCase(java.util.Locale.ENGLISH).replaceAll("[^a-z!]", ""));
return lookup.get(name.toLowerCase(Locale.ROOT).replaceAll("[^a-z!]", ""));
}
@Override

View file

@ -9,6 +9,7 @@ import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
@ -1086,7 +1087,7 @@ public final class PluginDescriptionFile {
if (map.get("load") != null) {
try {
order = PluginLoadOrder.valueOf(((String) map.get("load")).toUpperCase(java.util.Locale.ENGLISH).replaceAll("\\W", ""));
order = PluginLoadOrder.valueOf(((String) map.get("load")).toUpperCase(Locale.ROOT).replaceAll("\\W", ""));
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "load is of wrong type");
} catch (IllegalArgumentException ex) {

View file

@ -18,6 +18,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
@ -698,7 +699,7 @@ public final class SimplePluginManager implements PluginManager {
@Override
@Nullable
public Permission getPermission(@NotNull String name) {
return permissions.get(name.toLowerCase(java.util.Locale.ENGLISH));
return permissions.get(name.toLowerCase(Locale.ROOT));
}
@Override
@ -708,7 +709,7 @@ public final class SimplePluginManager implements PluginManager {
@Deprecated
public void addPermission(@NotNull Permission perm, boolean dirty) {
String name = perm.getName().toLowerCase(java.util.Locale.ENGLISH);
String name = perm.getName().toLowerCase(Locale.ROOT);
if (permissions.containsKey(name)) {
throw new IllegalArgumentException("The permission " + name + " is already defined!");
@ -731,12 +732,12 @@ public final class SimplePluginManager implements PluginManager {
@Override
public void removePermission(@NotNull String name) {
permissions.remove(name.toLowerCase(java.util.Locale.ENGLISH));
permissions.remove(name.toLowerCase(Locale.ROOT));
}
@Override
public void recalculatePermissionDefaults(@NotNull Permission perm) {
if (perm != null && permissions.containsKey(perm.getName().toLowerCase(java.util.Locale.ENGLISH))) {
if (perm != null && permissions.containsKey(perm.getName().toLowerCase(Locale.ROOT))) {
defaultPerms.get(true).remove(perm);
defaultPerms.get(false).remove(perm);
@ -775,7 +776,7 @@ public final class SimplePluginManager implements PluginManager {
@Override
public void subscribeToPermission(@NotNull String permission, @NotNull Permissible permissible) {
String name = permission.toLowerCase(java.util.Locale.ENGLISH);
String name = permission.toLowerCase(Locale.ROOT);
Map<Permissible, Boolean> map = permSubs.get(name);
if (map == null) {
@ -788,7 +789,7 @@ public final class SimplePluginManager implements PluginManager {
@Override
public void unsubscribeFromPermission(@NotNull String permission, @NotNull Permissible permissible) {
String name = permission.toLowerCase(java.util.Locale.ENGLISH);
String name = permission.toLowerCase(Locale.ROOT);
Map<Permissible, Boolean> map = permSubs.get(name);
if (map != null) {
@ -803,7 +804,7 @@ public final class SimplePluginManager implements PluginManager {
@Override
@NotNull
public Set<Permissible> getPermissionSubscriptions(@NotNull String permission) {
String name = permission.toLowerCase(java.util.Locale.ENGLISH);
String name = permission.toLowerCase(Locale.ROOT);
Map<Permissible, Boolean> map = permSubs.get(name);
if (map == null) {

View file

@ -12,6 +12,7 @@ import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Server;
@ -309,11 +310,11 @@ public abstract class JavaPlugin extends PluginBase {
*/
@Nullable
public PluginCommand getCommand(@NotNull String name) {
String alias = name.toLowerCase(java.util.Locale.ENGLISH);
String alias = name.toLowerCase(Locale.ROOT);
PluginCommand command = getServer().getPluginCommand(alias);
if (command == null || command.getPlugin() != this) {
command = getServer().getPluginCommand(description.getName().toLowerCase(java.util.Locale.ENGLISH) + ":" + alias);
command = getServer().getPluginCommand(description.getName().toLowerCase(Locale.ROOT) + ":" + alias);
}
if (command != null && command.getPlugin() == this) {

View file

@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import java.util.Locale;
import org.bukkit.Color;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
@ -348,7 +349,7 @@ public abstract class PotionEffectType implements Keyed, Translatable {
@Deprecated
public static PotionEffectType getByName(@NotNull String name) {
Preconditions.checkArgument(name != null, "name cannot be null");
return Registry.EFFECT.get(NamespacedKey.fromString(name.toLowerCase(java.util.Locale.ENGLISH)));
return Registry.EFFECT.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
}
/**

View file

@ -3,6 +3,7 @@ package org.bukkit;
import static org.bukkit.support.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Locale;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.jupiter.api.Test;
@ -31,7 +32,7 @@ public class ColorTest {
this.r = r;
this.g = g;
this.b = b;
this.name = id + ":" + Integer.toHexString(argb).toUpperCase() + "_" + Integer.toHexString(rgb).toUpperCase() + "_" + Integer.toHexString(bgr).toUpperCase() + "-a" + Integer.toHexString(a).toUpperCase() + "-r" + Integer.toHexString(r).toUpperCase() + "-g" + Integer.toHexString(g).toUpperCase() + "-b" + Integer.toHexString(b).toUpperCase();
this.name = id + ":" + Integer.toHexString(argb).toUpperCase(Locale.ROOT) + "_" + Integer.toHexString(rgb).toUpperCase(Locale.ROOT) + "_" + Integer.toHexString(bgr).toUpperCase(Locale.ROOT) + "-a" + Integer.toHexString(a).toUpperCase(Locale.ROOT) + "-r" + Integer.toHexString(r).toUpperCase(Locale.ROOT) + "-g" + Integer.toHexString(g).toUpperCase(Locale.ROOT) + "-b" + Integer.toHexString(b).toUpperCase(Locale.ROOT);
}
}

View file

@ -3,6 +3,7 @@ package org.bukkit;
import static org.bukkit.support.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Locale;
import org.bukkit.material.MaterialData;
import org.junit.jupiter.api.Test;
@ -66,7 +67,7 @@ public class MaterialTest {
@Test
public void matchMaterialByLowerCaseAndSpaces() {
for (Material material : Material.values()) {
String name = material.toString().replaceAll("_", " ").toLowerCase(java.util.Locale.ENGLISH);
String name = material.toString().replaceAll("_", " ").toLowerCase(Locale.ROOT);
assertThat(Material.matchMaterial(name), is(material));
}
}