From b6957db35267872998ad379064bb76fe455c096b Mon Sep 17 00:00:00 2001 From: vemacs Date: Thu, 4 Jun 2015 20:32:36 -0600 Subject: [PATCH] Apply patch "Fix float parsing issues" from Spigot-Essentials --- .../commands/Commandessentials.java | 3 +- .../essentials/commands/Commandsetworth.java | 5 ++- .../essentials/commands/Commandspeed.java | 3 +- .../essentials/commands/Commandtppos.java | 9 +++-- .../earth2me/essentials/utils/FloatUtil.java | 37 +++++++++++++++++++ 5 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/utils/FloatUtil.java diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java b/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java index 1fc454757..b94c5fe07 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandessentials.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.User; import com.earth2me.essentials.UserMap; import com.earth2me.essentials.metrics.Metrics; import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.FloatUtil; import com.earth2me.essentials.utils.NumberUtil; import com.google.common.base.Charsets; import org.bukkit.Material; @@ -199,7 +200,7 @@ public class Commandessentials extends EssentialsCommand { sender.sendMessage(tl("cleaning")); final long daysArg = Long.parseLong(args[1]); - final double moneyArg = args.length >= 3 ? Double.parseDouble(args[2].replaceAll("[^0-9\\.]", "")) : 0; + final double moneyArg = args.length >= 3 ? FloatUtil.parseDouble(args[2].replaceAll("[^0-9\\.]", "")) : 0; final int homesArg = args.length >= 4 && NumberUtil.isInt(args[3]) ? Integer.parseInt(args[3]) : 0; final UserMap userMap = ess.getUserMap(); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsetworth.java b/Essentials/src/com/earth2me/essentials/commands/Commandsetworth.java index d88b01695..efde7ee97 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsetworth.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsetworth.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.FloatUtil; import org.bukkit.Server; import org.bukkit.inventory.ItemStack; @@ -30,7 +31,7 @@ public class Commandsetworth extends EssentialsCommand { price = args[1]; } - ess.getWorth().setPrice(stack, Double.parseDouble(price)); + ess.getWorth().setPrice(stack, FloatUtil.parseDouble(price)); user.sendMessage(tl("worthSet")); } @@ -41,7 +42,7 @@ public class Commandsetworth extends EssentialsCommand { } ItemStack stack = ess.getItemDb().get(args[0]); - ess.getWorth().setPrice(stack, Double.parseDouble(args[1])); + ess.getWorth().setPrice(stack, FloatUtil.parseDouble(args[1])); sender.sendMessage(tl("worthSet")); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandspeed.java b/Essentials/src/com/earth2me/essentials/commands/Commandspeed.java index 00ce1c07b..0be3099f4 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandspeed.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandspeed.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.FloatUtil; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -107,7 +108,7 @@ public class Commandspeed extends EssentialsCommand { private float getMoveSpeed(final String moveSpeed) throws NotEnoughArgumentsException { float userSpeed; try { - userSpeed = Float.parseFloat(moveSpeed); + userSpeed = FloatUtil.parseFloat(moveSpeed); if (userSpeed > 10f) { userSpeed = 10f; } else if (userSpeed < 0.0001f) { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java index a6cec30e9..45b9f0037 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandtppos.java @@ -3,6 +3,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.FloatUtil; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -26,10 +27,10 @@ public class Commandtppos extends EssentialsCommand { final double z = args[2].startsWith("~") ? user.getLocation().getZ() + Integer.parseInt(args[2].substring(1)) : Integer.parseInt(args[2]); final Location loc = new Location(user.getWorld(), x, y, z, user.getLocation().getYaw(), user.getLocation().getPitch()); if (args.length > 3) { - loc.setYaw((Float.parseFloat(args[3]) + 180 + 360) % 360); + loc.setYaw((FloatUtil.parseFloat(args[3]) + 180 + 360) % 360); } if (args.length > 4) { - loc.setPitch(Float.parseFloat(args[4])); + loc.setPitch(FloatUtil.parseFloat(args[4])); } if (x > 30000000 || y > 30000000 || z > 30000000 || x < -30000000 || y < -30000000 || z < -30000000) { throw new NotEnoughArgumentsException(tl("teleportInvalidLocation")); @@ -53,10 +54,10 @@ public class Commandtppos extends EssentialsCommand { final double z = args[3].startsWith("~") ? user.getLocation().getZ() + Integer.parseInt(args[3].substring(1)) : Integer.parseInt(args[3]); final Location loc = new Location(user.getWorld(), x, y, z, user.getLocation().getYaw(), user.getLocation().getPitch()); if (args.length > 4) { - loc.setYaw((Float.parseFloat(args[4]) + 180 + 360) % 360); + loc.setYaw((FloatUtil.parseFloat(args[4]) + 180 + 360) % 360); } if (args.length > 5) { - loc.setPitch(Float.parseFloat(args[5])); + loc.setPitch(FloatUtil.parseFloat(args[5])); } if (x > 30000000 || y > 30000000 || z > 30000000 || x < -30000000 || y < -30000000 || z < -30000000) { throw new NotEnoughArgumentsException(tl("teleportInvalidLocation")); diff --git a/Essentials/src/com/earth2me/essentials/utils/FloatUtil.java b/Essentials/src/com/earth2me/essentials/utils/FloatUtil.java new file mode 100644 index 000000000..ba17a4133 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/FloatUtil.java @@ -0,0 +1,37 @@ +package com.earth2me.essentials.utils; + +/** + * parseFloat and parseDouble proxies that are protected against non-finite values. + */ +public class FloatUtil +{ + private FloatUtil() {} + + public static float parseFloat(String s) throws NumberFormatException + { + float f = Float.parseFloat(s); + if (Float.isNaN(f)) + { + throw new NumberFormatException("NaN is not valid"); + } + if (Float.isInfinite(f)) + { + throw new NumberFormatException("Infinity is not valid"); + } + return f; + } + + public static double parseDouble(String s) throws NumberFormatException + { + double d = Double.parseDouble(s); + if (Double.isNaN(d)) + { + throw new NumberFormatException("NaN is not valid"); + } + if (Double.isInfinite(d)) + { + throw new NumberFormatException("Infinity is not valid"); + } + return d; + } +} \ No newline at end of file