pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhai...@apache.org
Subject [pulsar] branch master updated: Issue #2751: Add check to fix NPE (#3034)
Date Sun, 25 Nov 2018 05:23:35 GMT
This is an automated email from the ASF dual-hosted git repository.

zhaijia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new c942479  Issue #2751: Add check to fix NPE (#3034)
c942479 is described below

commit c94247971765febc594cd60aa762f248624c2bc8
Author: Jia Zhai <jiazhai@users.noreply.github.com>
AuthorDate: Sun Nov 25 13:23:31 2018 +0800

    Issue #2751: Add check to fix NPE (#3034)
    
    Motivation
    We may meet NPE like this:
    ```
    java.lang.NullPointerException: null
    at org.apache.pulsar.common.api.proto.PulsarApi$KeyValue$Builder.setValue(PulsarApi.java:1923)
~[org.apache.pulsar-pulsar-common-2.1.1-incubating.jar:2.1.1-incubating]
    ```
    This is related to protobuf, it does not support null-able field directly.
    protocolbuffers/protobuf#1606
    
    In this fix we try to avoid this by add checking before this method is called.
---
 .../java/org/apache/pulsar/client/impl/TypedMessageBuilderImpl.java  | 5 +++++
 .../src/main/java/org/apache/pulsar/websocket/ProducerHandler.java   | 4 ++++
 2 files changed, 9 insertions(+)

diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TypedMessageBuilderImpl.java
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TypedMessageBuilderImpl.java
index 0b3f0a4..b0c116a 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TypedMessageBuilderImpl.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TypedMessageBuilderImpl.java
@@ -76,12 +76,15 @@ public class TypedMessageBuilderImpl<T> implements TypedMessageBuilder<T>
{
 
     @Override
     public TypedMessageBuilder<T> value(T value) {
+        checkArgument(value != null, "Need Non-Null content value");
         this.content = ByteBuffer.wrap(schema.encode(value));
         return this;
     }
 
     @Override
     public TypedMessageBuilder<T> property(String name, String value) {
+        checkArgument(name != null, "Need Non-Null name");
+        checkArgument(value != null, "Need Non-Null value for name: " + name);
         msgMetadataBuilder.addProperties(KeyValue.newBuilder().setKey(name).setValue(value).build());
         return this;
     }
@@ -89,6 +92,8 @@ public class TypedMessageBuilderImpl<T> implements TypedMessageBuilder<T>
{
     @Override
     public TypedMessageBuilder<T> properties(Map<String, String> properties)
{
         for (Map.Entry<String, String> entry : properties.entrySet()) {
+            checkArgument(entry.getKey() != null, "Need Non-Null key");
+            checkArgument(entry.getValue() != null, "Need Non-Null value for key: " + entry.getKey());
             msgMetadataBuilder
                     .addProperties(KeyValue.newBuilder().setKey(entry.getKey()).setValue(entry.getValue()).build());
         }
diff --git a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ProducerHandler.java
b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ProducerHandler.java
index e7954d5..bca9762 100644
--- a/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ProducerHandler.java
+++ b/pulsar-websocket/src/main/java/org/apache/pulsar/websocket/ProducerHandler.java
@@ -163,6 +163,10 @@ public class ProducerHandler extends AbstractWebSocketHandler {
             String msg = format("Invalid Base64 message-payload error=%s", e.getMessage());
             sendAckResponse(new ProducerAck(PayloadEncodingError, msg, null, requestContext));
             return;
+        } catch (NullPointerException e) {
+            // Null payload
+            sendAckResponse(new ProducerAck(PayloadEncodingError, e.getMessage(), null, requestContext));
+            return;
         }
 
         final long msgSize = rawPayload.length;


Mime
View raw message