Skip to content

Apache Lucene open-source search software

add basic bounds checks for terms/payloads/etc

Robert Muire9e37211528b
diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
index b0a68dce15b..24aca3fa663 100644
--- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
+++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java
@@ -769,6 +769,8 @@ public class CheckIndex {
break;
}
+ checkBounds(term);
+
// make sure terms arrive in order according to
// the comp
if (lastTerm == null) {
@@ -856,6 +858,9 @@ public class CheckIndex {
}
lastPos = pos;
BytesRef payload = postings.getPayload();
+ if (payload != null) {
+ checkBounds(payload);
+ }
if (payload != null && payload.length < 1) {
throw new RuntimeException("term " + term + ": doc " + doc + ": pos " + pos + " payload length is out of bounds " + payload.length);
}
@@ -1366,6 +1371,31 @@ public class CheckIndex {
}
return status;
}
+
+ // basic value checks
+ private static void checkBounds(BytesRef b) {
+ if (b.bytes == null) {
+ throw new RuntimeException("bytes is null");
+ }
+ if (b.length < 0) {
+ throw new RuntimeException("length is negative: " + b.length);
+ }
+ if (b.length > b.bytes.length) {
+ throw new RuntimeException("length is out of bounds: " + b.length + ", bytes.length=" + b.bytes.length);
+ }
+ if (b.offset < 0) {
+ throw new RuntimeException("offset is negative: " + b.offset);
+ }
+ if (b.offset > b.bytes.length) {
+ throw new RuntimeException("offset out of bounds: " + b.offset + ", length=" + b.length);
+ }
+ if (b.offset + b.length < 0) {
+ throw new RuntimeException("offset+length is negative: offset=" + b.offset + ",length=" + b.length);
+ }
+ if (b.offset + b.length > b.bytes.length) {
+ throw new RuntimeException("offset+length out of bounds: offset=" + b.offset + ",length=" + b.length + ",bytes.length=" + b.bytes.length);
+ }
+ }
/**
* Test term vectors for a segment.