LUCENE-8460: Better argument validation in StoredField

Signed-off-by: Namgyu Kim <kng0828@gmail.com>
Signed-off-by: Adrien Grand <jpountz@gmail.com>
This commit is contained in:
Namgyu Kim 2018-08-29 00:46:49 +09:00 committed by Adrien Grand
parent e2fc49cce2
commit a30eeae795
3 changed files with 33 additions and 22 deletions

View File

@ -272,6 +272,8 @@ Improvements
* LUCENE-8446: The UnifiedHighlighter's DefaultPassageFormatter now treats overlapping matches in * LUCENE-8446: The UnifiedHighlighter's DefaultPassageFormatter now treats overlapping matches in
the passage as merged (as if one larger match). (David Smiley) the passage as merged (as if one larger match). (David Smiley)
* LUCENE-8460: Better argument validation in StoredField. (Namgyu Kim)
Other: Other:
* LUCENE-8366: Upgrade to ICU 62.1. Emoji handling now uses Unicode 11's * LUCENE-8366: Upgrade to ICU 62.1. Emoji handling now uses Unicode 11's

View File

@ -169,9 +169,8 @@ public class Field implements IndexableField {
* @param name field name * @param name field name
* @param value byte array pointing to binary content (not copied) * @param value byte array pointing to binary content (not copied)
* @param type field type * @param type field type
* @throws IllegalArgumentException if the field name is null, * @throws IllegalArgumentException if the field name, value or type
* or the field's type is indexed() * is null, or the field's type is indexed().
* @throws NullPointerException if the type is null
*/ */
public Field(String name, byte[] value, IndexableFieldType type) { public Field(String name, byte[] value, IndexableFieldType type) {
this(name, value, 0, value.length, 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 offset starting position of the byte array
* @param length valid length of the byte array * @param length valid length of the byte array
* @param type field type * @param type field type
* @throws IllegalArgumentException if the field name is null, * @throws IllegalArgumentException if the field name, value or type
* or the field's type is indexed() * is null, or the field's type is indexed().
* @throws NullPointerException if the type is null
*/ */
public Field(String name, byte[] value, int offset, int length, IndexableFieldType type) { 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 name field name
* @param bytes BytesRef pointing to binary content (not copied) * @param bytes BytesRef pointing to binary content (not copied)
* @param type field type * @param type field type
* @throws IllegalArgumentException if the field name is null, * @throws IllegalArgumentException if the field name, bytes or type
* or the field's type is indexed() * is null, or the field's type is indexed().
* @throws NullPointerException if the type is null
*/ */
public Field(String name, BytesRef bytes, IndexableFieldType type) { public Field(String name, BytesRef bytes, IndexableFieldType type) {
if (name == null) { if (name == null) {
@ -214,9 +211,12 @@ public class Field implements IndexableField {
if (bytes == null) { if (bytes == null) {
throw new IllegalArgumentException("bytes must not be 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.fieldsData = bytes;
this.type = type; this.type = type;
this.name = name;
} }
// TODO: allow direct construction of int, long, float, double value too..? // 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 name field name
* @param value string value * @param value string value
* @param type field type * @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(), * is null, or if the field's type is neither indexed() nor stored(),
* or if indexed() is false but storeTermVectors() is true. * or if indexed() is false but storeTermVectors() is true.
* @throws NullPointerException if the type is null
*/ */
public Field(String name, String value, IndexableFieldType type) { public Field(String name, String value, IndexableFieldType type) {
if (name == null) { if (name == null) {
@ -238,13 +237,16 @@ public class Field implements IndexableField {
if (value == null) { if (value == null) {
throw new IllegalArgumentException("value must not be 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) { if (!type.stored() && type.indexOptions() == IndexOptions.NONE) {
throw new IllegalArgumentException("it doesn't make sense to have a field that " throw new IllegalArgumentException("it doesn't make sense to have a field that "
+ "is neither indexed nor stored"); + "is neither indexed nor stored");
} }
this.type = type;
this.name = name; this.name = name;
this.fieldsData = value; this.fieldsData = value;
this.type = type;
} }
/** /**

View File

@ -40,7 +40,8 @@ public class StoredField extends Field {
* FieldType}. * FieldType}.
* @param name field name * @param name field name
* @param type custom {@link FieldType} for this field * @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) { protected StoredField(String name, FieldType type) {
super(name, type); super(name, type);
@ -54,7 +55,8 @@ public class StoredField extends Field {
* @param name field name * @param name field name
* @param bytes byte array pointing to binary content (not copied) * @param bytes byte array pointing to binary content (not copied)
* @param type custom {@link FieldType} for this field * @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) { public StoredField(String name, BytesRef bytes, FieldType type) {
super(name, bytes, 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. * not to change it until you're done with this field.
* @param name field name * @param name field name
* @param value byte array pointing to binary content (not copied) * @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) { public StoredField(String name, byte[] value) {
super(name, value, TYPE); super(name, value, TYPE);
@ -80,7 +83,8 @@ public class StoredField extends Field {
* @param value byte array pointing to binary content (not copied) * @param value byte array pointing to binary content (not copied)
* @param offset starting position of the byte array * @param offset starting position of the byte array
* @param length valid length 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) { public StoredField(String name, byte[] value, int offset, int length) {
super(name, value, offset, length, TYPE); 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. * not to change it until you're done with this field.
* @param name field name * @param name field name
* @param value BytesRef pointing to binary content (not copied) * @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) { public StoredField(String name, BytesRef value) {
super(name, value, TYPE); super(name, value, TYPE);
@ -102,7 +107,8 @@ public class StoredField extends Field {
* Create a stored-only field with the given string value. * Create a stored-only field with the given string value.
* @param name field name * @param name field name
* @param value string value * @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) { public StoredField(String name, String value) {
super(name, value, TYPE); super(name, value, TYPE);
@ -114,7 +120,8 @@ public class StoredField extends Field {
* @param name field name * @param name field name
* @param value string value * @param value string value
* @param type custom {@link FieldType} for this field * @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) { public StoredField(String name, String value, FieldType type) {
super(name, value, type); super(name, value, type);