cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@apache.org
Subject [1/2] git commit: updated refs/heads/master to e1d4a32
Date Tue, 18 Nov 2014 13:14:14 GMT
Repository: cloudstack
Updated Branches:
  refs/heads/master 638da54fc -> e1d4a329c


Fix for potential NPE


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/16de4a71
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/16de4a71
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/16de4a71

Branch: refs/heads/master
Commit: 16de4a71761efe69053fefb6f77f16ddfff63c0f
Parents: 638da54
Author: Hugo Trippaers <htrippaers@schubergphilis.com>
Authored: Tue Nov 18 14:09:34 2014 +0100
Committer: Hugo Trippaers <htrippaers@schubergphilis.com>
Committed: Tue Nov 18 14:12:14 2014 +0100

----------------------------------------------------------------------
 utils/src/com/cloud/utils/net/NetUtils.java     | 45 ++++++++++----------
 .../test/com/cloud/utils/net/NetUtilsTest.java  | 35 ++++++++++-----
 2 files changed, 47 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/16de4a71/utils/src/com/cloud/utils/net/NetUtils.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java
index b092c94..3ce3697 100755
--- a/utils/src/com/cloud/utils/net/NetUtils.java
+++ b/utils/src/com/cloud/utils/net/NetUtils.java
@@ -19,16 +19,6 @@
 
 package com.cloud.utils.net;
 
-import com.cloud.utils.IteratorUtil;
-import com.cloud.utils.Pair;
-import com.cloud.utils.script.Script;
-import com.googlecode.ipv6.IPv6Address;
-import com.googlecode.ipv6.IPv6AddressRange;
-import com.googlecode.ipv6.IPv6Network;
-import org.apache.commons.lang.SystemUtils;
-import org.apache.commons.net.util.SubnetUtils;
-import org.apache.log4j.Logger;
-
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -51,6 +41,18 @@ import java.util.TreeSet;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.net.util.SubnetUtils;
+import org.apache.log4j.Logger;
+
+import com.googlecode.ipv6.IPv6Address;
+import com.googlecode.ipv6.IPv6AddressRange;
+import com.googlecode.ipv6.IPv6Network;
+
+import com.cloud.utils.IteratorUtil;
+import com.cloud.utils.Pair;
+import com.cloud.utils.script.Script;
+
 public class NetUtils {
     protected final static Logger s_logger = Logger.getLogger(NetUtils.class);
     public final static String HTTP_PORT = "80";
@@ -321,6 +323,8 @@ public class NetUtils {
             }
         } catch (SocketException e) {
             s_logger.error("SocketException when trying to retrieve MAC address", e);
+        } finally {
+            formatter.close();
         }
         return sb.toString();
     }
@@ -456,6 +460,7 @@ public class NetUtils {
         StringBuilder result = new StringBuilder(17);
         Formatter formatter = new Formatter(result);
         formatter.format("%02x:%02x:%02x:%02x:%02x:%02x", m[0], m[1], m[2], m[3], m[4], m[5]);
+        formatter.close();
         return result.toString();
     }
 
@@ -464,7 +469,7 @@ public class NetUtils {
         Formatter formatter = new Formatter(result);
         formatter.format("%02x:%02x:%02x:%02x:%02x:%02x", (macAddress >> 40) &
0xff, (macAddress >> 32) & 0xff, (macAddress >> 24) & 0xff, (macAddress
>> 16) & 0xff,
                 (macAddress >> 8) & 0xff, (macAddress & 0xff));
-
+        formatter.close();
         return result.toString();
     }
 
@@ -1307,20 +1312,14 @@ public class NetUtils {
         if (ips.length > 1) {
             endIp = ips[1];
         }
-        IPv6Address start, end;
         try {
-            start = IPv6Address.fromString(startIp);
-            end = IPv6Address.fromString(endIp);
-        } catch (IllegalArgumentException ex) {
-            return null;
-        }
-        BigInteger startInt = convertIPv6AddressToBigInteger(start);
-        BigInteger endInt = convertIPv6AddressToBigInteger(end);
-        if (endInt != null) {
-            if (startInt == null || startInt.compareTo(endInt) > 0) {
-                return null;
+            BigInteger startInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(startIp));
+            BigInteger endInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(endIp));
+            if (endInt != null && startInt != null && startInt.compareTo(endInt)
<= 0) {
+                return endInt.subtract(startInt).add(BigInteger.ONE);
             }
-            return endInt.subtract(startInt).add(BigInteger.ONE);
+        } catch (IllegalArgumentException ex) {
+            s_logger.error("Failed to convert a string to an IPv6 address", ex);
         }
         return null;
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/16de4a71/utils/test/com/cloud/utils/net/NetUtilsTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/net/NetUtilsTest.java b/utils/test/com/cloud/utils/net/NetUtilsTest.java
index 96c3baf..e48faa1 100644
--- a/utils/test/com/cloud/utils/net/NetUtilsTest.java
+++ b/utils/test/com/cloud/utils/net/NetUtilsTest.java
@@ -19,14 +19,6 @@
 
 package com.cloud.utils.net;
 
-import com.googlecode.ipv6.IPv6Address;
-import org.apache.log4j.Logger;
-import org.junit.Test;
-
-import java.math.BigInteger;
-import java.util.SortedSet;
-import java.util.TreeSet;
-
 import static org.hamcrest.Matchers.anyOf;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.equalTo;
@@ -39,6 +31,15 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
+import java.math.BigInteger;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.apache.log4j.Logger;
+import org.junit.Test;
+
+import com.googlecode.ipv6.IPv6Address;
+
 public class NetUtilsTest {
 
     private static final Logger s_logger = Logger.getLogger(NetUtilsTest.class);
@@ -138,8 +139,22 @@ public class NetUtilsTest {
 
     @Test
     public void testCountIp6InRange() {
-        assertEquals(NetUtils.countIp6InRange("1234:5678::1-1234:5678::2"), new BigInteger("2"));
-        assertEquals(NetUtils.countIp6InRange("1234:5678::2-1234:5678::0"), null);
+        assertEquals(new BigInteger("2"), NetUtils.countIp6InRange("1234:5678::1-1234:5678::2"));
+    }
+
+    @Test
+    public void testCountIp6InRangeWithInvalidRange() {
+        assertEquals(null, NetUtils.countIp6InRange("1234:5678::2-1234:5678::0"));
+    }
+
+    @Test
+    public void testCountIp6InRangeWithNullStart() {
+        assertEquals(null, NetUtils.countIp6InRange("-1234:5678::0"));
+    }
+
+    @Test
+    public void testCountIp6InRangeWithNoEnd() {
+        assertEquals(new BigInteger("1"), NetUtils.countIp6InRange("1234:5678::2"));
     }
 
     @Test


Mime
View raw message