Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

catch invalide usage of XXXPoint APIs, when dimensions are mismatched

Mike McCandless5d5b082e6d68
diff --git a/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java b/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java
index a7a63e0c34f..a12a0c9853a 100644
--- a/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/DoublePoint.java
@@ -42,6 +42,9 @@ public final class DoublePoint extends Field {
/** Change the values of this field */
public void setDoubleValues(double... point) {
+ if (type.pointDimensionCount() != point.length) {
+ throw new IllegalArgumentException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot change to (incoming) " + point.length + " dimensions");
+ }
fieldsData = pack(point);
}
@@ -52,6 +55,9 @@ public final class DoublePoint extends Field {
@Override
public Number numericValue() {
+ if (type.pointDimensionCount() != 1) {
+ throw new IllegalStateException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot convert to a single numeric value");
+ }
BytesRef bytes = (BytesRef) fieldsData;
assert bytes.length == RamUsageEstimator.NUM_BYTES_LONG;
return NumericUtils.sortableLongToDouble(NumericUtils.bytesToLongDirect(bytes.bytes, bytes.offset));
diff --git a/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java b/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java
index a023a4a82b5..00766ef9cb8 100644
--- a/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/FloatPoint.java
@@ -42,6 +42,9 @@ public final class FloatPoint extends Field {
/** Change the values of this field */
public void setFloatValues(float... point) {
+ if (type.pointDimensionCount() != point.length) {
+ throw new IllegalArgumentException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot change to (incoming) " + point.length + " dimensions");
+ }
fieldsData = pack(point);
}
@@ -52,6 +55,9 @@ public final class FloatPoint extends Field {
@Override
public Number numericValue() {
+ if (type.pointDimensionCount() != 1) {
+ throw new IllegalStateException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot convert to a single numeric value");
+ }
BytesRef bytes = (BytesRef) fieldsData;
assert bytes.length == RamUsageEstimator.NUM_BYTES_INT;
return NumericUtils.sortableIntToFloat(NumericUtils.bytesToIntDirect(bytes.bytes, bytes.offset));
diff --git a/lucene/core/src/java/org/apache/lucene/document/IntPoint.java b/lucene/core/src/java/org/apache/lucene/document/IntPoint.java
index 28f6a555472..ef487066967 100644
--- a/lucene/core/src/java/org/apache/lucene/document/IntPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/IntPoint.java
@@ -42,6 +42,9 @@ public final class IntPoint extends Field {
/** Change the values of this field */
public void setIntValues(int... point) {
+ if (type.pointDimensionCount() != point.length) {
+ throw new IllegalArgumentException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot change to (incoming) " + point.length + " dimensions");
+ }
fieldsData = pack(point);
}
@@ -52,6 +55,9 @@ public final class IntPoint extends Field {
@Override
public Number numericValue() {
+ if (type.pointDimensionCount() != 1) {
+ throw new IllegalStateException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot convert to a single numeric value");
+ }
BytesRef bytes = (BytesRef) fieldsData;
assert bytes.length == RamUsageEstimator.NUM_BYTES_INT;
return NumericUtils.bytesToInt(bytes.bytes, bytes.offset);
diff --git a/lucene/core/src/java/org/apache/lucene/document/LongPoint.java b/lucene/core/src/java/org/apache/lucene/document/LongPoint.java
index 23fddb28f0c..15810d1aafb 100644
--- a/lucene/core/src/java/org/apache/lucene/document/LongPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/LongPoint.java
@@ -42,6 +42,9 @@ public final class LongPoint extends Field {
/** Change the values of this field */
public void setLongValues(long... point) {
+ if (type.pointDimensionCount() != point.length) {
+ throw new IllegalArgumentException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot change to (incoming) " + point.length + " dimensions");
+ }
fieldsData = pack(point);
}
@@ -52,6 +55,9 @@ public final class LongPoint extends Field {
@Override
public Number numericValue() {
+ if (type.pointDimensionCount() != 1) {
+ throw new IllegalStateException("this field (name=" + name + ") uses " + type.pointDimensionCount() + " dimensions; cannot convert to a single numeric value");
+ }
BytesRef bytes = (BytesRef) fieldsData;
assert bytes.length == RamUsageEstimator.NUM_BYTES_LONG;
return NumericUtils.bytesToLong(bytes.bytes, bytes.offset);
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestPointValues.java b/lucene/core/src/test/org/apache/lucene/index/TestPointValues.java
index 738109e36ce..ff5fdf883e0 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestPointValues.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestPointValues.java
@@ -21,8 +21,11 @@ import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.document.BinaryPoint;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.Field;
+import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntPoint;
+import org.apache.lucene.document.LongPoint;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
@@ -435,4 +438,72 @@ public class TestPointValues extends LuceneTestCase {
w.close();
dir.close();
}
+
+ public void testInvalidIntPointUsage() throws Exception {
+ IntPoint field = new IntPoint("field", 17, 42);
+ try {
+ field.setIntValue(14);
+ fail("did not hit exception");
+ } catch (IllegalArgumentException iae) {
+ // good
+ }
+
+ try {
+ field.numericValue();
+ fail("did not hit exception");
+ } catch (IllegalStateException ise) {
+ // good
+ }
+ }
+
+ public void testInvalidLongPointUsage() throws Exception {
+ LongPoint field = new LongPoint("field", 17, 42);
+ try {
+ field.setLongValue(14);
+ fail("did not hit exception");
+ } catch (IllegalArgumentException iae) {
+ // good
+ }
+
+ try {
+ field.numericValue();
+ fail("did not hit exception");
+ } catch (IllegalStateException ise) {
+ // good
+ }
+ }
+
+ public void testInvalidFloatPointUsage() throws Exception {
+ FloatPoint field = new FloatPoint("field", 17, 42);
+ try {
+ field.setFloatValue(14);
+ fail("did not hit exception");
+ } catch (IllegalArgumentException iae) {
+ // good
+ }
+
+ try {
+ field.numericValue();
+ fail("did not hit exception");
+ } catch (IllegalStateException ise) {
+ // good
+ }
+ }
+
+ public void testInvalidDoublePointUsage() throws Exception {
+ DoublePoint field = new DoublePoint("field", 17, 42);
+ try {
+ field.setDoubleValue(14);
+ fail("did not hit exception");
+ } catch (IllegalArgumentException iae) {
+ // good
+ }
+
+ try {
+ field.numericValue();
+ fail("did not hit exception");
+ } catch (IllegalStateException ise) {
+ // good
+ }
+ }
}