From e99d1fa6688c7a80dbd026be961d8ed12bbe5088 Mon Sep 17 00:00:00 2001 From: DaCool <8727384-DaCool@users.noreply.gitlab.com> Date: Sun, 1 Oct 2023 16:30:02 +0000 Subject: [PATCH] Fix numeric overflow Make multiplication cumulative (handy since it's already a switch case) Convert and use uppercase Also remove unused statusIsOK method, or as Clou calls it, dead code --- src/main/java/com/sheepit/client/Utils.java | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/sheepit/client/Utils.java b/src/main/java/com/sheepit/client/Utils.java index 416e2fc..da96803 100644 --- a/src/main/java/com/sheepit/client/Utils.java +++ b/src/main/java/com/sheepit/client/Utils.java @@ -255,23 +255,16 @@ public class Utils { } final Matcher m = Pattern.compile("([\\d.,]+)\\s*(\\w)").matcher(in); m.find(); - int scale = 1; - switch (m.group(2).charAt(0)) { + long scale = 1; + switch (Character.toUpperCase(m.group(2).charAt(0))) { case 'T': - case 't': - scale = 1000 * 1000 * 1000 * 1000; - break; - case 'G': - case 'g': - scale = 1000 * 1000 * 1000; - break; + scale *= 1000; //Multiply by 1000 each time the higher the metric prefix + case 'G': //Note the lack of break statements, thus it executes through + scale *= 1000; case 'M': - case 'm': - scale = 1000 * 1000; - break; + scale *= 1000; case 'K': - case 'k': - scale = 1000; + scale *= 1000; break; } return Math.round(Double.parseDouble(m.group(1)) * scale);