From b04f8fe1598f84af0187e2f4521f195df8e8044c Mon Sep 17 00:00:00 2001 From: Jay Modi Date: Mon, 9 Jan 2017 09:19:05 -0500 Subject: [PATCH] prevent NPE when calling GetResponse#getSourceAsBytesRef This commit checks for a null BytesReference as the value for `source` in GetResult#sourceRef and simply returns null. Previously this would have resulted in a NPE. While this does seem internal at first glance, it can affect user code as a GetResponse could trigger this when the document is missing. Additionally, the CompressorFactory#uncompressIfNeeded now requires a non-null argument. --- .../common/compress/CompressorFactory.java | 4 +++- .../java/org/elasticsearch/index/get/GetResult.java | 6 +++++- .../org/elasticsearch/index/get/GetResultTests.java | 11 +++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java b/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java index 067d4666722..a355a12d672 100644 --- a/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java +++ b/core/src/main/java/org/elasticsearch/common/compress/CompressorFactory.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; +import java.util.Objects; public class CompressorFactory { @@ -68,9 +69,10 @@ public class CompressorFactory { /** * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(BytesReference)}. + * @throws NullPointerException a NullPointerException will be thrown when bytes is null */ public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException { - Compressor compressor = compressor(bytes); + Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null")); BytesReference uncompressed; if (compressor != null) { uncompressed = uncompress(bytes, compressor); diff --git a/core/src/main/java/org/elasticsearch/index/get/GetResult.java b/core/src/main/java/org/elasticsearch/index/get/GetResult.java index 2618efddae6..78c3b78fe61 100644 --- a/core/src/main/java/org/elasticsearch/index/get/GetResult.java +++ b/core/src/main/java/org/elasticsearch/index/get/GetResult.java @@ -54,7 +54,7 @@ public class GetResult implements Streamable, Iterable, ToXContentObje private static final String _VERSION = "_version"; private static final String FOUND = "found"; private static final String FIELDS = "fields"; - + private String index; private String type; private String id; @@ -135,6 +135,10 @@ public class GetResult implements Streamable, Iterable, ToXContentObje * Returns bytes reference, also un compress the source if needed. */ public BytesReference sourceRef() { + if (source == null) { + return null; + } + try { this.source = CompressorFactory.uncompressIfNeeded(this.source); return this.source; diff --git a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java index 958f6ac095a..0d0008cd1ce 100644 --- a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java @@ -79,6 +79,17 @@ public class GetResultTests extends ESTestCase { } } + public void testGetSourceAsBytes() { + XContentType xContentType = randomFrom(XContentType.values()); + Tuple tuple = randomGetResult(xContentType); + GetResult getResult = tuple.v1(); + if (getResult.isExists()) { + assertNotNull(getResult.sourceRef()); + } else { + assertNull(getResult.sourceRef()); + } + } + public void testEqualsAndHashcode() { checkEqualsAndHashCode(randomGetResult(XContentType.JSON).v1(), GetResultTests::copyGetResult, GetResultTests::mutateGetResult); }