# HG changeset patch # User Tom Rodriguez # Date 1422647784 28800 # Node ID c370e6f3957528e89454dc4db63b455cbef8d3f7 # Parent 7d805868d01d8d7f07b9435f013993473854c37c Protect against violation of Map interface contract diff -r 7d805868d01d -r c370e6f39575 graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeMap.java --- 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 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++; } } diff -r 7d805868d01d -r c370e6f39575 graal/com.oracle.graal.graph/src/com/oracle/graal/graph/NodeNodeMap.java --- 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 keySet() { HashSet entries = new HashSet<>(); - for (Map.Entry 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 values() { @@ -78,6 +89,11 @@ for (Map.Entry 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); } }