You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2014/05/09 18:36:25 UTC
svn commit: r1593577 - in /lucene/dev/trunk/lucene/core/src:
java/org/apache/lucene/document/Field.java
test/org/apache/lucene/index/TestIndexWriterExceptions.java
Author: rmuir
Date: Fri May 9 16:36:25 2014
New Revision: 1593577
URL: http://svn.apache.org/r1593577
Log:
LUCENE-5662: add missing null checks to Field to avoid aborting exceptions
Modified:
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/document/Field.java
lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/document/Field.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/document/Field.java?rev=1593577&r1=1593576&r2=1593577&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/document/Field.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/document/Field.java Fri May 9 16:36:25 2014
@@ -211,6 +211,9 @@ public class Field implements IndexableF
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
+ if (bytes == null) {
+ throw new IllegalArgumentException("bytes cannot be null");
+ }
if (type.indexed()) {
throw new IllegalArgumentException("Fields with BytesRef values cannot be indexed");
}
@@ -299,6 +302,9 @@ public class Field implements IndexableF
if (!(fieldsData instanceof String)) {
throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to String");
}
+ if (value == null) {
+ throw new IllegalArgumentException("value cannot be null");
+ }
fieldsData = value;
}
@@ -335,6 +341,9 @@ public class Field implements IndexableF
if (type.indexed()) {
throw new IllegalArgumentException("cannot set a BytesRef value on an indexed field");
}
+ if (value == null) {
+ throw new IllegalArgumentException("value cannot be null");
+ }
fieldsData = value;
}
Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java?rev=1593577&r1=1593576&r2=1593577&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java Fri May 9 16:36:25 2014
@@ -42,6 +42,7 @@ import org.apache.lucene.document.FieldT
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.SortedSetDocValuesField;
+import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
@@ -1488,6 +1489,149 @@ public class TestIndexWriterExceptions e
dir.close();
}
+ /** test a null string value doesn't abort the entire segment */
+ public void testNullStoredField() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer analyzer = new MockAnalyzer(random());
+ IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
+ // add good document
+ Document doc = new Document();
+ iw.addDocument(doc);
+ try {
+ // set to null value
+ String value = null;
+ doc.add(new StoredField("foo", value));
+ iw.addDocument(doc);
+ } catch (IllegalArgumentException expected) {}
+ iw.shutdown();
+ // make sure we see our good doc
+ DirectoryReader r = DirectoryReader.open(dir);
+ assertEquals(1, r.numDocs());
+ r.close();
+ dir.close();
+ }
+
+ /** test a null string value doesn't abort the entire segment */
+ public void testNullStoredFieldReuse() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer analyzer = new MockAnalyzer(random());
+ IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
+ // add good document
+ Document doc = new Document();
+ Field theField = new StoredField("foo", "hello", StoredField.TYPE);
+ doc.add(theField);
+ iw.addDocument(doc);
+ try {
+ // set to null value
+ theField.setStringValue(null);
+ iw.addDocument(doc);
+ } catch (IllegalArgumentException expected) {}
+ iw.shutdown();
+ // make sure we see our good doc
+ DirectoryReader r = DirectoryReader.open(dir);
+ assertEquals(1, r.numDocs());
+ r.close();
+ dir.close();
+ }
+
+ /** test a null byte[] value doesn't abort the entire segment */
+ public void testNullStoredBytesField() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer analyzer = new MockAnalyzer(random());
+ IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
+ // add good document
+ Document doc = new Document();
+ iw.addDocument(doc);
+
+ try {
+ // set to null value
+ byte v[] = null;
+ Field theField = new StoredField("foo", v);
+ doc.add(theField);
+ iw.addDocument(doc);
+ } catch (NullPointerException expected) {}
+ iw.shutdown();
+ // make sure we see our good doc
+ DirectoryReader r = DirectoryReader.open(dir);
+ assertEquals(1, r.numDocs());
+ r.close();
+ dir.close();
+ }
+
+ /** test a null byte[] value doesn't abort the entire segment */
+ public void testNullStoredBytesFieldReuse() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer analyzer = new MockAnalyzer(random());
+ IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
+ // add good document
+ Document doc = new Document();
+ Field theField = new StoredField("foo", new BytesRef("hello").bytes);
+ doc.add(theField);
+ iw.addDocument(doc);
+ try {
+ // set to null value
+ byte v[] = null;
+ theField.setBytesValue(v);
+ iw.addDocument(doc);
+ } catch (NullPointerException expected) {}
+ iw.shutdown();
+ // make sure we see our good doc
+ DirectoryReader r = DirectoryReader.open(dir);
+ assertEquals(1, r.numDocs());
+ r.close();
+ dir.close();
+ }
+
+ /** test a null bytesref value doesn't abort the entire segment */
+ public void testNullStoredBytesRefField() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer analyzer = new MockAnalyzer(random());
+ IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
+ // add good document
+ Document doc = new Document();
+ iw.addDocument(doc);
+
+ try {
+ // set to null value
+ BytesRef v = null;
+ Field theField = new StoredField("foo", v);
+ doc.add(theField);
+ iw.addDocument(doc);
+ } catch (IllegalArgumentException expected) {}
+ iw.shutdown();
+ // make sure we see our good doc
+ DirectoryReader r = DirectoryReader.open(dir);
+ assertEquals(1, r.numDocs());
+ r.close();
+ dir.close();
+ }
+
+ /** test a null bytesref value doesn't abort the entire segment */
+ public void testNullStoredBytesRefFieldReuse() throws Exception {
+ Directory dir = newDirectory();
+ Analyzer analyzer = new MockAnalyzer(random());
+ IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
+ // add good document
+ Document doc = new Document();
+ Field theField = new StoredField("foo", new BytesRef("hello"));
+ doc.add(theField);
+ iw.addDocument(doc);
+ try {
+ // set to null value
+ BytesRef v = null;
+ theField.setBytesValue(v);
+ iw.addDocument(doc);
+ } catch (IllegalArgumentException expected) {}
+ iw.shutdown();
+ // make sure we see our good doc
+ DirectoryReader r = DirectoryReader.open(dir);
+ assertEquals(1, r.numDocs());
+ r.close();
+ dir.close();
+ }
+
+ // TODO: we could also check isValid, to catch "broken" bytesref values, might be too much?
+
static class UOEDirectory extends RAMDirectory {
boolean doFail = false;