diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java index 7fa937e3d83..ea32d3ed61f 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java @@ -382,19 +382,12 @@ public class Bytes { } public static byte [] toBytesBinary(String in) { - // this may be bigger than we need, but lets be safe. + // this may be bigger than we need, but let's be safe. byte [] b = new byte[in.length()]; int size = 0; for (int i = 0; i < in.length(); ++i) { char ch = in.charAt(i); - if (ch == '\\') { - // begin hex escape: - char next = in.charAt(i+1); - if (next != 'x') { - // invalid escape sequence, ignore this one. - b[size++] = (byte)ch; - continue; - } + if (ch == '\\' && in.length() > i+1 && in.charAt(i+1) == 'x') { // ok, take next 2 hex digits. char hd1 = in.charAt(i+2); char hd2 = in.charAt(i+3); diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java index cf7f7e0dd25..da4a5669142 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java @@ -290,5 +290,13 @@ public class TestBytes extends TestCase { assertFalse(bytes == copy); assertTrue(Bytes.equals(bytes, copy)); } + + public void testToBytesBinaryTrailingBackslashes() throws Exception { + try { + Bytes.toBytesBinary("abc\\x00\\x01\\"); + } catch (StringIndexOutOfBoundsException ex) { + fail("Illegal string access: " + ex.getMessage()); + } + } }