changeset 221:cbfff74807d5

8133660: I2CDeviceConfig.Builder.setAddress(-1, 10) does not result in IAE as specified Summary: Fix I2CDeviceConfig.Builder.setAddress() check of address and addressSize Reviewed-by: snazarki Contributed-by: olga.milovskaya@oracle.com
author snazarki
date Fri, 14 Aug 2015 18:41:06 +0300
parents f4b2abb4ddd3
children 810d9321e203
files src/share/classes/jdk/dio/i2cbus/I2CDeviceConfig.java
diffstat 1 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/jdk/dio/i2cbus/I2CDeviceConfig.java	Fri Aug 14 18:38:57 2015 +0300
+++ b/src/share/classes/jdk/dio/i2cbus/I2CDeviceConfig.java	Fri Aug 14 18:41:06 2015 +0300
@@ -156,8 +156,8 @@
          * </ul>
          */
         public Builder setAddress(int address, int addressSize) {
-            Utils.checkIntValue(address);
-            checkAddressSize(addressSize);
+            Utils.checkIntZeroOrPozitive(address);
+            checkAddressSize(address, addressSize);
             instance.address = address;
             instance.addressSize = addressSize;
             return this;
@@ -445,17 +445,27 @@
     }
 
 
-    private static void checkAddressSize(int addressSize) {
-        if (ADDR_SIZE_7 != addressSize && ADDR_SIZE_10 != addressSize && UNASSIGNED != addressSize) {
+    private static void checkAddressSize(int address, int addressSize) {
+        if (ADDR_SIZE_7 != addressSize && ADDR_SIZE_10 != addressSize) {
             throw new IllegalArgumentException(Integer.toString(addressSize));
         }
+
+        int expAddressSize = 2;
+
+        for (int i = 0; i < addressSize - 1; i++) {
+            expAddressSize = expAddressSize * 2;
+        }
+
+        if (address >= expAddressSize) {
+            throw new IllegalArgumentException(Integer.toString(address));
+        }
     }
 
     private void checkValues() {
         Utils.checkIntValue(controllerNumber);
-        Utils.checkIntValue(address);
+        Utils.checkIntZeroOrPozitive(address);
         Utils.checkGreaterThanZero(clockFrequency);
-        checkAddressSize(addressSize);
+        checkAddressSize(address, addressSize);
     }
 
 }