From a30eeae7956c8c59037ca1c08e7f69474da10e7a Mon Sep 17 00:00:00 2001 From: Namgyu Kim Date: Wed, 29 Aug 2018 00:46:49 +0900 Subject: [PATCH] LUCENE-8460: Better argument validation in StoredField Signed-off-by: Namgyu Kim Signed-off-by: Adrien Grand --- lucene/CHANGES.txt | 2 ++ .../org/apache/lucene/document/Field.java | 30 ++++++++++--------- .../apache/lucene/document/StoredField.java | 23 +++++++++----- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 409419fe025..a9f93b9523f 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -272,6 +272,8 @@ Improvements * LUCENE-8446: The UnifiedHighlighter's DefaultPassageFormatter now treats overlapping matches in the passage as merged (as if one larger match). (David Smiley) +* LUCENE-8460: Better argument validation in StoredField. (Namgyu Kim) + Other: * LUCENE-8366: Upgrade to ICU 62.1. Emoji handling now uses Unicode 11's diff --git a/lucene/core/src/java/org/apache/lucene/document/Field.java b/lucene/core/src/java/org/apache/lucene/document/Field.java index cbb559a60d8..467fec7c237 100644 --- a/lucene/core/src/java/org/apache/lucene/document/Field.java +++ b/lucene/core/src/java/org/apache/lucene/document/Field.java @@ -169,9 +169,8 @@ public class Field implements IndexableField { * @param name field name * @param value byte array pointing to binary content (not copied) * @param type field type - * @throws IllegalArgumentException if the field name is null, - * or the field's type is indexed() - * @throws NullPointerException if the type is null + * @throws IllegalArgumentException if the field name, value or type + * is null, or the field's type is indexed(). */ public Field(String name, byte[] value, IndexableFieldType type) { this(name, value, 0, value.length, type); @@ -187,12 +186,11 @@ public class Field implements IndexableField { * @param offset starting position of the byte array * @param length valid length of the byte array * @param type field type - * @throws IllegalArgumentException if the field name is null, - * or the field's type is indexed() - * @throws NullPointerException if the type is null + * @throws IllegalArgumentException if the field name, value or type + * is null, or the field's type is indexed(). */ public Field(String name, byte[] value, int offset, int length, IndexableFieldType type) { - this(name, new BytesRef(value, offset, length), type); + this(name, value != null ? new BytesRef(value, offset, length) : null, type); } /** @@ -203,9 +201,8 @@ public class Field implements IndexableField { * @param name field name * @param bytes BytesRef pointing to binary content (not copied) * @param type field type - * @throws IllegalArgumentException if the field name is null, - * or the field's type is indexed() - * @throws NullPointerException if the type is null + * @throws IllegalArgumentException if the field name, bytes or type + * is null, or the field's type is indexed(). */ public Field(String name, BytesRef bytes, IndexableFieldType type) { if (name == null) { @@ -214,9 +211,12 @@ public class Field implements IndexableField { if (bytes == null) { throw new IllegalArgumentException("bytes must not be null"); } + if (type == null) { + throw new IllegalArgumentException("type must not be null"); + } + this.name = name; this.fieldsData = bytes; this.type = type; - this.name = name; } // TODO: allow direct construction of int, long, float, double value too..? @@ -226,10 +226,9 @@ public class Field implements IndexableField { * @param name field name * @param value string value * @param type field type - * @throws IllegalArgumentException if either the name or value + * @throws IllegalArgumentException if either the name, value or type * is null, or if the field's type is neither indexed() nor stored(), * or if indexed() is false but storeTermVectors() is true. - * @throws NullPointerException if the type is null */ public Field(String name, String value, IndexableFieldType type) { if (name == null) { @@ -238,13 +237,16 @@ public class Field implements IndexableField { if (value == null) { throw new IllegalArgumentException("value must not be null"); } + if (type == null) { + throw new IllegalArgumentException("type must not be null"); + } if (!type.stored() && type.indexOptions() == IndexOptions.NONE) { throw new IllegalArgumentException("it doesn't make sense to have a field that " + "is neither indexed nor stored"); } - this.type = type; this.name = name; this.fieldsData = value; + this.type = type; } /** diff --git a/lucene/core/src/java/org/apache/lucene/document/StoredField.java b/lucene/core/src/java/org/apache/lucene/document/StoredField.java index 12b529c232a..7dc5a99f5be 100644 --- a/lucene/core/src/java/org/apache/lucene/document/StoredField.java +++ b/lucene/core/src/java/org/apache/lucene/document/StoredField.java @@ -40,12 +40,13 @@ public class StoredField extends Field { * FieldType}. * @param name field name * @param type custom {@link FieldType} for this field - * @throws IllegalArgumentException if the field name is null. + * @throws IllegalArgumentException if the field name or type + * is null. */ protected StoredField(String name, FieldType type) { super(name, type); } - + /** * Expert: allows you to customize the {@link * FieldType}. @@ -54,7 +55,8 @@ public class StoredField extends Field { * @param name field name * @param bytes byte array pointing to binary content (not copied) * @param type custom {@link FieldType} for this field - * @throws IllegalArgumentException if the field name is null. + * @throws IllegalArgumentException if the field name, value or type + * is null. */ public StoredField(String name, BytesRef bytes, FieldType type) { super(name, bytes, type); @@ -66,7 +68,8 @@ public class StoredField extends Field { * not to change it until you're done with this field. * @param name field name * @param value byte array pointing to binary content (not copied) - * @throws IllegalArgumentException if the field name is null. + * @throws IllegalArgumentException if the field name or value + * is null. */ public StoredField(String name, byte[] value) { super(name, value, TYPE); @@ -80,7 +83,8 @@ public class StoredField extends Field { * @param value byte array pointing to binary content (not copied) * @param offset starting position of the byte array * @param length valid length of the byte array - * @throws IllegalArgumentException if the field name is null. + * @throws IllegalArgumentException if the field name or value + * is null. */ public StoredField(String name, byte[] value, int offset, int length) { super(name, value, offset, length, TYPE); @@ -92,7 +96,8 @@ public class StoredField extends Field { * not to change it until you're done with this field. * @param name field name * @param value BytesRef pointing to binary content (not copied) - * @throws IllegalArgumentException if the field name is null. + * @throws IllegalArgumentException if the field name or value + * is null. */ public StoredField(String name, BytesRef value) { super(name, value, TYPE); @@ -102,7 +107,8 @@ public class StoredField extends Field { * Create a stored-only field with the given string value. * @param name field name * @param value string value - * @throws IllegalArgumentException if the field name or value is null. + * @throws IllegalArgumentException if the field name or value + * is null. */ public StoredField(String name, String value) { super(name, value, TYPE); @@ -114,7 +120,8 @@ public class StoredField extends Field { * @param name field name * @param value string value * @param type custom {@link FieldType} for this field - * @throws IllegalArgumentException if the field name or value is null. + * @throws IllegalArgumentException if the field name, value or type + * is null. */ public StoredField(String name, String value, FieldType type) { super(name, value, type);