You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2018/08/30 20:02:00 UTC

[2/3] lucene-solr:branch_7x: LUCENE-8460: Better argument validation in StoredField

LUCENE-8460: Better argument validation in StoredField

Signed-off-by: Namgyu Kim <kn...@gmail.com>
Signed-off-by: Adrien Grand <jp...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/b1d165c6
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/b1d165c6
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/b1d165c6

Branch: refs/heads/branch_7x
Commit: b1d165c6e8d4d3e78a7c7222ff634779a9677d5b
Parents: de381da
Author: Namgyu Kim <kn...@gmail.com>
Authored: Wed Aug 29 00:46:49 2018 +0900
Committer: Adrien Grand <jp...@gmail.com>
Committed: Thu Aug 30 17:46:19 2018 +0200

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  2 ++
 .../java/org/apache/lucene/document/Field.java  | 30 +++++++++++---------
 .../org/apache/lucene/document/StoredField.java | 23 +++++++++------
 3 files changed, 33 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b1d165c6/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 5890649..f8dfd55 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -132,6 +132,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

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b1d165c6/lucene/core/src/java/org/apache/lucene/document/Field.java
----------------------------------------------------------------------
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 cbb559a..467fec7 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;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/b1d165c6/lucene/core/src/java/org/apache/lucene/document/StoredField.java
----------------------------------------------------------------------
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 12b529c..7dc5a99 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);