From fea49cb8409167636a94e25f5657aeee58587f6e Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Wed, 24 Nov 2010 21:30:00 +0000 Subject: [PATCH] THRIFT-1008. java: byte[] accessors throw NPE on unset field This patch adds a null check to TBaseHelper.rightSize(). git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1038833 13f79535-47bb-0310-9956-ffa450edef68 --- lib/java/src/org/apache/thrift/TBaseHelper.java | 3 +++ lib/java/test/org/apache/thrift/TestTBaseHelper.java | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/lib/java/src/org/apache/thrift/TBaseHelper.java b/lib/java/src/org/apache/thrift/TBaseHelper.java index 2837d0b5..5898d5b2 100644 --- a/lib/java/src/org/apache/thrift/TBaseHelper.java +++ b/lib/java/src/org/apache/thrift/TBaseHelper.java @@ -270,6 +270,9 @@ public final class TBaseHelper { } public static ByteBuffer rightSize(ByteBuffer in) { + if (in == null) { + return null; + } if (wrapsFullArray(in)) { return in; } diff --git a/lib/java/test/org/apache/thrift/TestTBaseHelper.java b/lib/java/test/org/apache/thrift/TestTBaseHelper.java index 6d72ad84..6378524f 100644 --- a/lib/java/test/org/apache/thrift/TestTBaseHelper.java +++ b/lib/java/test/org/apache/thrift/TestTBaseHelper.java @@ -151,6 +151,10 @@ public class TestTBaseHelper extends TestCase { assertEquals(ByteBuffer.wrap(b1, 1, 3), ByteBuffer.wrap(b3)); } + public void testRightSize() throws Exception { + assertNull(TBaseHelper.rightSize(null)); + } + public void testCopyBinaryWithByteBuffer() throws Exception { byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5}; ByteBuffer b = ByteBuffer.wrap(bytes); -- 2.17.1