From ff2d15aced231bdf2c8955a4e9f554c0631f38c3 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 10 Oct 2012 13:57:36 -0700 Subject: [PATCH] [JENKINS-15439] adding more probe. --- .../model/lazy/AbstractLazyLoadRunMap.java | 19 +++++-- .../lazy/AbstractLazyLoadRunMapTest.java | 50 +++++++++++++------ .../jenkins/model/lazy/FakeMapBuilder.java | 1 + .../jenkins/model/lazy/SortedIntListTest.java | 17 +++++++ 4 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 core/src/test/java/jenkins/model/lazy/SortedIntListTest.java diff --git a/core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java b/core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java index 9d76a29ff7..85ecebaa6e 100644 --- a/core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java +++ b/core/src/main/java/jenkins/model/lazy/AbstractLazyLoadRunMap.java @@ -398,19 +398,30 @@ public abstract class AbstractLazyLoadRunMap extends AbstractMap i // We know that the build we are looking for exists in [lo,hi) --- it's "hi)" and not "hi]" because we do +1. // we will narrow this down via binary search + final int initialSize = idOnDisk.size(); int lo = idOnDisk.higher(cid); int hi = idOnDisk.lower(fid)+1; final int initialLo = lo, initialHi = hi; + if (!(0<=lo && lo<=hi && hi<=idOnDisk.size())) { + // assertion error, but we are so far unable to get to the bottom of this bug. + // but don't let this kill the loading the hard way + String msg = String.format( + "Assertion error: failing to load #%d %s: lo=%d,hi=%d,size=%d,size2=%d", + n, d, lo, hi, idOnDisk.size(), initialSize); + LOGGER.log(Level.WARNING, msg,new Exception()); + throw new ArrayIndexOutOfBoundsException(msg); + } + while (lo extends AbstractMap i // assertion error, but we are so far unable to get to the bottom of this bug. // but don't let this kill the loading the hard way LOGGER.log(Level.WARNING, String.format( - "Assertion error: failing to load #%d %s: lo=%d,hi=%d,size=%d (initial:lo=%d,hi=%d)", - n,d,lo,hi,idOnDisk.size(), initialLo,initialHi),new Exception()); + "Assertion error: failing to load #%d %s: lo=%d,hi=%d,size=%d (initial:lo=%d,hi=%d,size=%d)", + n,d,lo,hi,idOnDisk.size(), initialLo,initialHi,initialSize),new Exception()); return null; } return getById(idOnDisk.get(lo-1)); diff --git a/core/src/test/java/jenkins/model/lazy/AbstractLazyLoadRunMapTest.java b/core/src/test/java/jenkins/model/lazy/AbstractLazyLoadRunMapTest.java index 8cd648cd36..647b0356a7 100644 --- a/core/src/test/java/jenkins/model/lazy/AbstractLazyLoadRunMapTest.java +++ b/core/src/test/java/jenkins/model/lazy/AbstractLazyLoadRunMapTest.java @@ -35,6 +35,7 @@ import java.util.NoSuchElementException; import java.util.SortedMap; import java.util.logging.Level; import org.junit.BeforeClass; +import org.jvnet.hudson.test.Bug; /** * @author Kohsuke Kawaguchi @@ -68,11 +69,11 @@ public class AbstractLazyLoadRunMapTest extends Assert { @Test public void lookup() { assertNull(a.get(0)); - a.get(1).asserts(1,"A"); + a.get(1).asserts(1, "A"); assertNull(a.get(2)); - a.get(3).asserts(3,"B"); + a.get(3).asserts(3, "B"); assertNull(a.get(4)); - a.get(5).asserts(5,"C"); + a.get(5).asserts(5, "C"); assertNull(a.get(6)); assertNull(b.get(1)); @@ -126,7 +127,7 @@ public class AbstractLazyLoadRunMapTest extends Assert { @Test public void search() { // searching toward non-existent direction - assertNull(a.search( 99, Direction.ASC)); + assertNull(a.search(99, Direction.ASC)); assertNull(a.search(-99, Direction.DESC)); } @@ -137,17 +138,17 @@ public class AbstractLazyLoadRunMapTest extends Assert { public void unloadableData() throws IOException { FakeMap m = localBuilder.add(1, "A").addUnloadable("B").add(5, "C").make(); - assertNull(m.search(3,Direction.EXACT)); - m.search(3,Direction.DESC).asserts(1,"A"); - m.search(3, Direction.ASC ).asserts(5,"C"); + assertNull(m.search(3, Direction.EXACT)); + m.search(3,Direction.DESC).asserts(1, "A"); + m.search(3, Direction.ASC ).asserts(5, "C"); } @Test public void eagerLoading() throws IOException { Map.Entry[] b = a.entrySet().toArray(new Map.Entry[3]); - ((Build)b[0].getValue()).asserts(5,"C"); - ((Build)b[1].getValue()).asserts(3,"B"); - ((Build)b[2].getValue()).asserts(1,"A"); + ((Build)b[0].getValue()).asserts(5, "C"); + ((Build)b[1].getValue()).asserts(3, "B"); + ((Build)b[2].getValue()).asserts(1, "A"); } @Test @@ -163,7 +164,7 @@ public class AbstractLazyLoadRunMapTest extends Assert { @Test public void fastSearch() throws IOException { - FakeMap a = localBuilder.addBoth(1, "A").addBoth(3, "B").addBoth(5, "C").addBoth(7,"D").make(); + FakeMap a = localBuilder.addBoth(1, "A").addBoth(3, "B").addBoth(5, "C").addBoth(7, "D").make(); // we should be using the cache to find the entry efficiently a.search(6, Direction.ASC).asserts(7,"D"); @@ -178,7 +179,7 @@ public class AbstractLazyLoadRunMapTest extends Assert { @Test public void bogusCacheAndHiddenRealData() throws IOException { - FakeMap a = localBuilder.addUnloadableCache(1).add(1,"A").make(); + FakeMap a = localBuilder.addUnloadableCache(1).add(1, "A").make(); a.get(1).asserts(1, "A"); } @@ -219,9 +220,9 @@ public class AbstractLazyLoadRunMapTest extends Assert { assertEquals(2, m.size()); Build[] b = m.values().toArray(new Build[2]); - assertEquals(2,b.length); - b[0].asserts(5,"C"); - b[1].asserts(3,"B"); + assertEquals(2, b.length); + b[0].asserts(5, "C"); + b[1].asserts(3, "B"); } @Test @@ -231,4 +232,23 @@ public class AbstractLazyLoadRunMapTest extends Assert { a.hashCode(); b.hashCode(); } + + @Bug(15439) + @Test + public void indexOutOfBounds() throws Exception { + FakeMapBuilder f = localBuilder; + f.add(100,"A") + .addUnloadable("B") + .addUnloadable("C") + .addUnloadable("D") + .addUnloadable("E") + .addUnloadable("F") + .addUnloadable("G") + .add(200,"H") + .add(201,"I"); + FakeMap map = f.make(); + + Build x = map.search(Integer.MAX_VALUE, Direction.DESC); + assert x.n==201; + } } diff --git a/core/src/test/java/jenkins/model/lazy/FakeMapBuilder.java b/core/src/test/java/jenkins/model/lazy/FakeMapBuilder.java index 0288094245..db4c3db1ea 100644 --- a/core/src/test/java/jenkins/model/lazy/FakeMapBuilder.java +++ b/core/src/test/java/jenkins/model/lazy/FakeMapBuilder.java @@ -100,6 +100,7 @@ public class FakeMapBuilder implements TestRule { } public FakeMap make() { + assert dir!=null; return new FakeMap(dir); } diff --git a/core/src/test/java/jenkins/model/lazy/SortedIntListTest.java b/core/src/test/java/jenkins/model/lazy/SortedIntListTest.java new file mode 100644 index 0000000000..8fcd92354b --- /dev/null +++ b/core/src/test/java/jenkins/model/lazy/SortedIntListTest.java @@ -0,0 +1,17 @@ +package jenkins.model.lazy; + +import org.junit.Test; + +/** + * @author Kohsuke Kawaguchi + */ +public class SortedIntListTest { + @Test + public void testLower() { + SortedIntList l = new SortedIntList(5); + l.add(0); + l.add(5); + l.add(10); + System.out.println(l.lower(Integer.MAX_VALUE)); + } +}