Author: sebb
Date: Fri Mar 10 23:33:40 2017
New Revision: 1786469
URL: http://svn.apache.org/viewvc?rev=1786469&view=rev
Log:
NET-624 SubnetInfo#toCidrNotation - a wrong format subnet mask is allowed
Modified:
commons/proper/net/trunk/src/changes/changes.xml
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
Modified: commons/proper/net/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1786469&r1=1786468&r2=1786469&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Fri Mar 10
23:33:40 2017
@@ -71,6 +71,9 @@ This is mainly a bug-fix release. See fu
However it is not source compatible with releases before 3.4, as some methods
were added to the interface NtpV3Packet in 3.4
">
+ <action issue="NET-624" type="fix" dev="sebb" due-to="Makoto
Sakaguchi">
+ SubnetInfo#toCidrNotation: A wrong format subnet mask is allowed
+ </action>
<action issue="NET-623" type="fix" dev="sebb" due-to="Makoto
Sakaguchi">
SubnetUtils - fixed spelling errors
</action>
Modified:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java?rev=1786469&r1=1786468&r2=1786469&view=diff
==============================================================================
---
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
(original)
+++
commons/proper/net/trunk/src/main/java/org/apache/commons/net/util/SubnetUtils.java
Fri Mar 10 23:33:40 2017
@@ -354,11 +354,25 @@ public class SubnetUtils {
return x & 0x0000003F;
}
- /* Convert two dotted decimal addresses to a single xxx.xxx.xxx.xxx/yy
format
+ /*
+ * Convert two dotted decimal addresses to a single xxx.xxx.xxx.xxx/yy
format
* by counting the 1-bit population in the mask address. (It may be better
to count
* NBITS-#trailing zeroes for this case)
*/
private String toCidrNotation(String addr, String mask) {
- return addr + "/" + pop(toInteger(mask));
+ int maskInt = toInteger(mask);
+
+ /*
+ * Check the subnet mask
+ *
+ * An IPv4 subnet mask must consist of a set of contiguous 1-bits
followed by a block of 0-bits.
+ * If the mask follows the format, the numbers of subtracting one from
the lowest one bit of the mask,
+ * see Hacker's Delight section 2.1, equals to the bitwise complement
of the mask.
+ */
+ if ((maskInt & -maskInt) - 1 != ~maskInt) {
+ throw new IllegalArgumentException("Could not parse [" + mask +
"]");
+ }
+
+ return addr + "/" + pop(maskInt);
}
}
Modified:
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java?rev=1786469&r1=1786468&r2=1786469&view=diff
==============================================================================
---
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
(original)
+++
commons/proper/net/trunk/src/test/java/org/apache/commons/net/SubnetUtilsTest.java
Fri Mar 10 23:33:40 2017
@@ -340,6 +340,24 @@ public class SubnetUtilsTest extends Tes
assertEquals(2147483646, info.getAddressCount());
}
+ public void testNET624() {
+ new SubnetUtils("0.0.0.0/0");
+ new SubnetUtils("0.0.0.0","0.0.0.0");
+ new SubnetUtils("0.0.0.0","128.0.0.0");
+ try {
+ new SubnetUtils("0.0.0.0","64.0.0.0");
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+ // Ignored
+ }
+ try {
+ new SubnetUtils("0.0.0.0","0.0.0.1");
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException expected) {
+ // Ignored
+ }
+ }
+
public void testNET520() {
SubnetUtils utils = new SubnetUtils("0.0.0.0/0");
utils.setInclusiveHostCount(true);