You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by va...@apache.org on 2018/09/01 05:53:46 UTC

lucene-solr:master: SOLR-12704: Guard AddSchemaFieldsUpdateProcessorFactory against null field names and field values

Repository: lucene-solr
Updated Branches:
  refs/heads/master e7b449534 -> d55a81df8


SOLR-12704: Guard AddSchemaFieldsUpdateProcessorFactory against null field names and field values


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

Branch: refs/heads/master
Commit: d55a81df849e093d043ac21f9e98532f091694d9
Parents: e7b4495
Author: Varun Thacker <va...@apache.org>
Authored: Fri Aug 31 23:53:30 2018 -0600
Committer: Varun Thacker <va...@apache.org>
Committed: Fri Aug 31 23:53:30 2018 -0600

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  3 ++
 .../AddSchemaFieldsUpdateProcessorFactory.java  | 36 +++++++++++++-------
 ...dSchemaFieldsUpdateProcessorFactoryTest.java | 15 ++++++++
 3 files changed, 41 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d55a81df/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index f0d49ea..94b12be4 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -295,6 +295,9 @@ Bug Fixes
 
 * SOLR-11585: Solr SQL does not work with point numeric fields (Joel Bernstein, Kiran Chitturi)
 
+* SOLR-12704: Guard AddSchemaFieldsUpdateProcessorFactory against null field names and field values.
+  (Steve Rowe, Varun Thacker)
+
 Optimizations
 ----------------------
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d55a81df/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java
index 6b8f1d5..131d72c 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java
@@ -481,13 +481,18 @@ public class AddSchemaFieldsUpdateProcessorFactory extends UpdateRequestProcesso
     private void getUnknownFields
     (FieldNameSelector selector, SolrInputDocument doc, Map<String,List<SolrInputField>> unknownFields) {
       for (final String fieldName : doc.getFieldNames()) {
-        if (selector.shouldMutate(fieldName)) { // returns false if the field already exists in the current schema
-          List<SolrInputField> solrInputFields = unknownFields.get(fieldName);
-          if (null == solrInputFields) {
-            solrInputFields = new ArrayList<>();
-            unknownFields.put(fieldName, solrInputFields);
+        //We do a assert and a null check because even after SOLR-12710 is addressed
+        //older SolrJ versions can send null values causing an NPE
+        assert fieldName != null;
+        if (fieldName != null) {
+          if (selector.shouldMutate(fieldName)) { // returns false if the field already exists in the current schema
+            List<SolrInputField> solrInputFields = unknownFields.get(fieldName);
+            if (null == solrInputFields) {
+              solrInputFields = new ArrayList<>();
+              unknownFields.put(fieldName, solrInputFields);
+            }
+            solrInputFields.add(doc.getField(fieldName));
           }
-          solrInputFields.add(doc.getField(fieldName));
         }
       }
       List<SolrInputDocument> childDocs = doc.getChildDocuments();
@@ -506,15 +511,20 @@ public class AddSchemaFieldsUpdateProcessorFactory extends UpdateRequestProcesso
     private TypeMapping mapValueClassesToFieldType(List<SolrInputField> fields) {
       NEXT_TYPE_MAPPING: for (TypeMapping typeMapping : typeMappings) {
         for (SolrInputField field : fields) {
-          NEXT_FIELD_VALUE: for (Object fieldValue : field.getValues()) {
-            for (Class<?> valueClass : typeMapping.valueClasses) {
-              if (valueClass.isInstance(fieldValue)) {
-                continue NEXT_FIELD_VALUE;
+          //We do a assert and a null check because even after SOLR-12710 is addressed
+          //older SolrJ versions can send null values causing an NPE
+          assert field.getValues() != null;
+          if (field.getValues() != null) {
+            NEXT_FIELD_VALUE: for (Object fieldValue : field.getValues()) {
+              for (Class<?> valueClass : typeMapping.valueClasses) {
+                if (valueClass.isInstance(fieldValue)) {
+                  continue NEXT_FIELD_VALUE;
+                }
               }
+              // This fieldValue is not an instance of any of the mapped valueClass-s,
+              // so mapping fails - go try the next type mapping.
+              continue NEXT_TYPE_MAPPING;
             }
-            // This fieldValue is not an instance of any of the mapped valueClass-s,
-            // so mapping fails - go try the next type mapping.
-            continue NEXT_TYPE_MAPPING;
           }
         }
         // Success! Each of this field's values is an instance of a mapped valueClass

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d55a81df/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java b/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java
index 96cc676..a49e9fe 100644
--- a/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java
+++ b/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java
@@ -59,6 +59,21 @@ public class AddSchemaFieldsUpdateProcessorFactoryTest extends UpdateProcessorTe
     initCore(SOLRCONFIG_XML, SCHEMA_XML, tmpSolrHome.getPath());
   }
 
+  public void testEmptyValue() {
+    IndexSchema schema = h.getCore().getLatestSchema();
+    final String fieldName = "newFieldABC";
+    assertNull(schema.getFieldOrNull(fieldName));
+    //UpdateProcessorTestBase#doc doesn't deal with nulls
+    SolrInputDocument doc = new SolrInputDocument();
+    doc.addField("id", "1");
+    doc.addField(fieldName, null);
+
+    SolrInputDocument finalDoc = doc;
+    expectThrows(AssertionError.class, () -> processAdd("add-fields-no-run-processor", finalDoc));
+
+    expectThrows(AssertionError.class, () -> processAdd("add-fields-no-run-processor", new SolrInputDocument(null , null)));
+  }
+
   public void testSingleField() throws Exception {
     IndexSchema schema = h.getCore().getLatestSchema();
     final String fieldName = "newfield1";