From 0bc127e0bd1d46bb3fc1e9f1d9c22566cf0c7d99 Mon Sep 17 00:00:00 2001 From: Josh Elser Date: Mon, 20 Apr 2015 22:59:41 -0400 Subject: [PATCH] HBASE-13520 Avoid NPE in TagRewriteCell When a TagRewriteCell makes the optimization to avoid a reference to a byte-array that is no longer used, it needs to ensure that it doesn't attempt to compute the tags length on the original array (which may be null). --- .../org/apache/hadoop/hbase/TagRewriteCell.java | 4 ++ .../apache/hadoop/hbase/TestTagRewriteCell.java | 48 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/TestTagRewriteCell.java diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/TagRewriteCell.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/TagRewriteCell.java index 313ecb8..e6412f7 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/TagRewriteCell.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/TagRewriteCell.java @@ -143,6 +143,10 @@ public class TagRewriteCell implements Cell, SettableSequenceId, SettableTimesta @Override public int getTagsLength() { + if (null == this.tags) { + // Nulled out tags array optimization in constructor + return 0; + } return this.tags.length; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestTagRewriteCell.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestTagRewriteCell.java new file mode 100644 index 0000000..56cecf4 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestTagRewriteCell.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase; + +import static org.junit.Assert.assertTrue; + +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(SmallTests.class) +public class TestTagRewriteCell { + + @Test + public void testHeapSize() { + Cell originalCell = CellUtil.createCell(Bytes.toBytes("row"), Bytes.toBytes("value")); + final int fakeTagArrayLength = 10; + TagRewriteCell trCell = new TagRewriteCell(originalCell, new byte[fakeTagArrayLength]); + + // Get the heapSize before the internal tags array in trCell are nuked + long trCellHeapSize = trCell.heapSize(); + + // Make another TagRewriteCell with the original TagRewriteCell + // This happens on systems with more than one RegionObserver/Coproc loaded (such as + // VisibilityController and AccessController) + TagRewriteCell trCell2 = new TagRewriteCell(trCell, new byte[fakeTagArrayLength]); + + assertTrue("TagRewriteCell containing a TagRewriteCell's heapsize should be larger than a " + + "single TagRewriteCell's heapsize", trCellHeapSize < trCell2.heapSize()); + assertTrue("TagRewriteCell should have had nulled out tags array", trCell.heapSize() < + trCellHeapSize); + } +} -- 2.1.2