This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new 6557ad3c60e minor: HttpSolrClient NPE avoidance (#3629)
6557ad3c60e is described below

commit 6557ad3c60efe0852c8380bc7601fc808019fae3
Author: David Smiley <[email protected]>
AuthorDate: Wed Sep 10 00:36:04 2025 -0400

    minor: HttpSolrClient NPE avoidance (#3629)
    
    This PR fixes a potential NPE in HttpSolrClient by deferring the call to 
entity.getContent() until after null checks are performed and providing a safe 
fallback when the entity is null.
    
    * Moves entity.getContent() call after null checks to avoid NPE
    * Adds null-safe fallback using empty ByteArrayInputStream when entity is 
null
    * Removes unnecessary early declaration of respBody variable
---
 .../src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java  | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git 
a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java 
b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
index 3f6e5a28c85..b5de3894268 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java
@@ -18,6 +18,7 @@ package org.apache.solr.client.solrj.impl;
 
 import static org.apache.solr.common.util.Utils.getObjectByPath;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -550,7 +551,6 @@ public class HttpSolrClient extends BaseHttpSolrClient {
     method.setConfig(requestConfigBuilder.build());
 
     HttpEntity entity = null;
-    InputStream respBody = null;
     boolean shouldClose = true;
     try {
       // Execute the method.
@@ -568,7 +568,6 @@ public class HttpSolrClient extends BaseHttpSolrClient {
 
       // Read the contents
       entity = response.getEntity();
-      respBody = entity.getContent();
       String mimeType = null;
       Charset charset = null;
       String charsetName = null;
@@ -608,6 +607,9 @@ public class HttpSolrClient extends BaseHttpSolrClient {
                 null);
           }
       }
+
+      InputStream respBody =
+          (entity == null ? new ByteArrayInputStream(new byte[0]) : 
entity.getContent());
       if (processor == null || processor instanceof InputStreamResponseParser) 
{
         // no processor specified, return raw stream
         final var rsp =

Reply via email to