From 8eed45f5117065995cbceb7ae2a96c9057a806bd Mon Sep 17 00:00:00 2001 From: James Moger Date: Mon, 6 Oct 2014 10:33:56 -0400 Subject: Be more careful with primitive type rollover in alias instantiation --- releases.moxie | 2 ++ src/main/java/com/iciql/util/Utils.java | 16 ++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/releases.moxie b/releases.moxie index 31b1b86..53a19a0 100644 --- a/releases.moxie +++ b/releases.moxie @@ -12,12 +12,14 @@ r21: { fixes: - Return null NPE in selectFirst() if list is empty (pr-5) - Fix Moxie toolkit download URL (pr-6) + - Be more careful with primitive numeric type rollovers changes: ~ additions: - Add syntax for IN and NOT IN (pr-7) - Add support for nested AND/OR conditions (pr-8) dependencyChanges: ~ contributors: + - James Moger - Kazunobu Raita - Sotaro Suzuki } diff --git a/src/main/java/com/iciql/util/Utils.java b/src/main/java/com/iciql/util/Utils.java index bc636d1..92ca825 100644 --- a/src/main/java/com/iciql/util/Utils.java +++ b/src/main/java/com/iciql/util/Utils.java @@ -49,19 +49,19 @@ import com.iciql.IciqlException; public class Utils { public static final AtomicLong COUNTER = new AtomicLong(0); - + public static final AtomicInteger AS_COUNTER = new AtomicInteger(0); private static final boolean MAKE_ACCESSIBLE = true; private static final int BUFFER_BLOCK_SIZE = 4 * 1024; - + public static synchronized int nextAsCount() { // prevent negative values and use a threadsafe counter int count = AS_COUNTER.incrementAndGet(); if (count == Integer.MAX_VALUE) { count = 0; - AS_COUNTER.set(count); + AS_COUNTER.set(count); } return count; } @@ -146,15 +146,15 @@ public class Utils { public static T newObject(Class clazz) { // must create new instances if (clazz == int.class || clazz == Integer.class) { - return (T) new Integer((int) COUNTER.getAndIncrement()); + return (T) new Integer((int) (COUNTER.getAndIncrement() % Integer.MAX_VALUE)); } else if (clazz == String.class) { return (T) ("" + COUNTER.getAndIncrement()); } else if (clazz == long.class || clazz == Long.class) { return (T) new Long(COUNTER.getAndIncrement()); } else if (clazz == short.class || clazz == Short.class) { - return (T) new Short((short) COUNTER.getAndIncrement()); + return (T) new Short((short) (COUNTER.getAndIncrement() % Short.MAX_VALUE)); } else if (clazz == byte.class || clazz == Byte.class) { - return (T) new Byte((byte) COUNTER.getAndIncrement()); + return (T) new Byte((byte) (COUNTER.getAndIncrement() % Byte.MAX_VALUE)); } else if (clazz == float.class || clazz == Float.class) { return (T) new Float(COUNTER.getAndIncrement()); } else if (clazz == double.class || clazz == Double.class) { @@ -397,7 +397,7 @@ public class Utils { /** * Read a number of characters from a reader and close it. - * + * * @param in * the reader * @param length @@ -430,7 +430,7 @@ public class Utils { /** * Read a number of bytes from a stream and close it. - * + * * @param in * the stream * @param length -- cgit v1.2.3