changeset 19061:c370e6f39575

Protect against violation of Map interface contract
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Fri, 30 Jan 2015 11:56:24 -0800
parents 7d805868d01d
children a8bcda325946
files graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeMap.java graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeNodeMap.java
diffstat 2 files changed, 30 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeMap.java	Fri Jan 30 11:33:32 2015 -0800
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeMap.java	Fri Jan 30 11:56:24 2015 -0800
@@ -98,6 +98,14 @@
         values[getNodeId(node)] = value;
     }
 
+    /**
+     * @param i
+     * @return Return the key for the entry at index {@code i}
+     */
+    protected Node getKey(int i) {
+        return graph.getNode(i);
+    }
+
     public int size() {
         return values.length;
     }
@@ -135,7 +143,7 @@
                     @Override
                     public Entry<Node, T> next() {
                         final int pos = i;
-                        Node key = NodeMap.this.graph.getNode(pos);
+                        Node key = NodeMap.this.getKey(pos);
                         T value = (T) NodeMap.this.values[pos];
                         i++;
                         forward();
@@ -158,7 +166,7 @@
                     }
 
                     private void forward() {
-                        while (i < NodeMap.this.values.length && (NodeMap.this.graph.getNode(i) == null || NodeMap.this.values[i] == null)) {
+                        while (i < NodeMap.this.values.length && (NodeMap.this.getKey(i) == null || NodeMap.this.values[i] == null)) {
                             i++;
                         }
                     }
--- a/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeNodeMap.java	Fri Jan 30 11:33:32 2015 -0800
+++ b/graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeNodeMap.java	Fri Jan 30 11:56:24 2015 -0800
@@ -56,10 +56,21 @@
 
     public Set<Node> keySet() {
         HashSet<Node> entries = new HashSet<>();
-        for (Map.Entry<Node, Node> entry : entries()) {
-            entries.add(entry.getKey());
+        for (int i = 0; i < values.length; ++i) {
+            Object v = values[i];
+            if (v != null) {
+                Node key = getKey(i);
+                if (key != null) {
+                    entries.add(key);
+                }
+            }
         }
-        return entries;
+        /*
+         * The normal contract for entrySet is that modifications of the set are reflected in the
+         * underlying data structure. For simplicity don't allow that but complain if someone tries
+         * to use it that way.
+         */
+        return Collections.unmodifiableSet(entries);
     }
 
     public Collection<Node> values() {
@@ -78,6 +89,11 @@
         for (Map.Entry<Node, Node> entry : entries()) {
             entries.add(entry);
         }
-        return entries;
+        /*
+         * The normal contract for entrySet is that modifications of the set are reflected in the
+         * underlying data structure. For simplicity don't allow that but complain if someone tries
+         * to use it that way.
+         */
+        return Collections.unmodifiableSet(entries);
     }
 }