From 3218387f7f2cc88c2430f55ddc9caf5205050a63 Mon Sep 17 00:00:00 2001 From: Lukas Rytz Date: Thu, 16 Jul 2015 15:32:42 +0200 Subject: [PATCH] Fix findItemByIndex in case of hash collisions The method `findItemByIndex` only looked at the first item in each bucket of the `items` hash table. Also, in case the item was not found, the counter `i` would be at `items.length`, therefore the access `items[i]` crashed with an ArrayIndexOutOfBoundsException. Fixes the issue https://github.com/scala/scala-asm/issues/8. --- src/main/java/scala/tools/asm/ClassWriter.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/scala/tools/asm/ClassWriter.java b/src/main/java/scala/tools/asm/ClassWriter.java index 5c2de3f..a8cebb9 100644 --- a/src/main/java/scala/tools/asm/ClassWriter.java +++ b/src/main/java/scala/tools/asm/ClassWriter.java @@ -1747,12 +1747,20 @@ private void put(final Item i) { } /** - * Find item that whose index is `index`. + * Returns the item with a specific index. + * + * @param index + * the index of the searched item. + * @return the item with the given index. */ public Item findItemByIndex(int index) { - int i = 0; - while (i < items.length && (items[i] == null || items[i].index != index)) i++; - return items[i]; + for (Item item : items) { + while (item != null) { + if (item.index == index) return item; + item = item.next; + } + } + return null; } /**