Bug 508939 - Improve error detection when the index is cleared during
indexing 

Change-Id: Iadbcd5a8c7195b572fd441021918fb18d4e78182
Signed-off-by: Stefan Xenos <sxenos@gmail.com>
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/Nd.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/Nd.java
index 948049d..3b6694b 100644
--- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/Nd.java
+++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/Nd.java
@@ -25,7 +25,7 @@
 /**
  * Network Database for storing semantic information.
  */
-public class Nd {
+public final class Nd {
 	private static final int CANCELLATION_CHECK_INTERVAL = 500;
 	private static final int BLOCKED_WRITE_LOCK_OUTPUT_INTERVAL = 30000;
 	private static final int LONG_WRITE_LOCK_REPORT_THRESHOLD = 1000;
@@ -615,4 +615,8 @@
 	public void clear(IProgressMonitor monitor) {
 		getDB().clear(getDefaultVersion());
 	}
+
+	public boolean isValidAddress(long address) {
+		return address > 0 && address < getDB().getChunkCount() * Database.CHUNK_SIZE;
+	}
 }
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/java/NdResourceFile.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/java/NdResourceFile.java
index 14e2e53..358fcf8 100644
--- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/java/NdResourceFile.java
+++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/java/NdResourceFile.java
@@ -81,7 +81,7 @@
 			Nd nd = getNd();
 			// In the common case where the resource file was deleted and the memory hasn't yet been reused,
 			// this will fail.
-			if (NODE_TYPE.get(nd, this.address) != nd.getNodeType(getClass())) {
+			if (!nd.isValidAddress(this.address) || NODE_TYPE.get(nd, this.address) != nd.getNodeType(getClass())) {
 				return false;
 			}