cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tylerho...@apache.org
Subject [1/3] cassandra git commit: Avoid NPE when serializing ErrorMessage with null msg
Date Tue, 23 Feb 2016 17:30:03 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk fc9c6faa2 -> babf30dd1


Avoid NPE when serializing ErrorMessage with null msg

Patch by Tyler Hobbs; reviewed by Carl Yeksigian for CASSANDRA-11167


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

Branch: refs/heads/trunk
Commit: 50095947e25f630ce48ee24d10ff3e1f3fd91183
Parents: c8c8cf6
Author: Tyler Hobbs <tylerlhobbs@gmail.com>
Authored: Tue Feb 23 11:28:17 2016 -0600
Committer: Tyler Hobbs <tylerlhobbs@gmail.com>
Committed: Tue Feb 23 11:28:17 2016 -0600

----------------------------------------------------------------------
 CHANGES.txt                                       |  1 +
 .../transport/messages/ErrorMessage.java          |  6 ++++--
 .../cassandra/transport/ProtocolErrorTest.java    | 18 ++++++++++++++++++
 3 files changed, 23 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/50095947/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 767eb8a..01e7b3d 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.2.6
+ * Avoid NPE when serializing ErrorMessage with null message (CASSANDRA-11167)
  * Replacing an aggregate with a new version doesn't reset INITCOND (CASSANDRA-10840)
  * (cqlsh) cqlsh cannot be called through symlink (CASSANDRA-11037)
  * fix ohc and java-driver pom dependencies in build.xml (CASSANDRA-10793)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/50095947/src/java/org/apache/cassandra/transport/messages/ErrorMessage.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/transport/messages/ErrorMessage.java b/src/java/org/apache/cassandra/transport/messages/ErrorMessage.java
index 222e833..021db5a 100644
--- a/src/java/org/apache/cassandra/transport/messages/ErrorMessage.java
+++ b/src/java/org/apache/cassandra/transport/messages/ErrorMessage.java
@@ -151,7 +151,8 @@ public class ErrorMessage extends Message.Response
         {
             final TransportException err = getBackwardsCompatibleException(msg, version);
             dest.writeInt(err.code().value);
-            CBUtil.writeString(err.getMessage(), dest);
+            String errorString = err.getMessage() == null ? "" : err.getMessage();
+            CBUtil.writeString(errorString, dest);
 
             switch (err.code())
             {
@@ -212,7 +213,8 @@ public class ErrorMessage extends Message.Response
         public int encodedSize(ErrorMessage msg, int version)
         {
             final TransportException err = getBackwardsCompatibleException(msg, version);
-            int size = 4 + CBUtil.sizeOfString(err.getMessage());
+            String errorString = err.getMessage() == null ? "" : err.getMessage();
+            int size = 4 + CBUtil.sizeOfString(errorString);
             switch (err.code())
             {
                 case UNAVAILABLE:

http://git-wip-us.apache.org/repos/asf/cassandra/blob/50095947/test/unit/org/apache/cassandra/transport/ProtocolErrorTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/transport/ProtocolErrorTest.java b/test/unit/org/apache/cassandra/transport/ProtocolErrorTest.java
index 11b0ebd..fc8c41c 100644
--- a/test/unit/org/apache/cassandra/transport/ProtocolErrorTest.java
+++ b/test/unit/org/apache/cassandra/transport/ProtocolErrorTest.java
@@ -113,4 +113,22 @@ public class ProtocolErrorTest {
             Assert.assertTrue(e.getMessage().contains("Request is too big"));
         }
     }
+
+    @Test
+    public void testErrorMessageWithNullString() throws Exception
+    {
+        // test for CASSANDRA-11167
+        ErrorMessage msg = ErrorMessage.fromException(new ServerError((String) null));
+        assert msg.toString().endsWith("null") : msg.toString();
+        int size = ErrorMessage.codec.encodedSize(msg, Server.CURRENT_VERSION);
+        ByteBuf buf = Unpooled.buffer(size);
+        ErrorMessage.codec.encode(msg, buf, Server.CURRENT_VERSION);
+
+        ByteBuf expected = Unpooled.wrappedBuffer(new byte[]{
+                0x00, 0x00, 0x00, 0x00,  // int error code
+                0x00, 0x00               // short message length
+        });
+
+        Assert.assertEquals(expected, buf);
+    }
 }


Mime
View raw message