From 0c13da349bd12bbf2568b69a22d73dd1cbe01798 Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Wed, 12 Sep 2012 19:51:19 +0000 Subject: [PATCH] HBASE-6518 Bytes.toBytesBinary() incorrect trailing backslash escape git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1384103 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/java/org/apache/hadoop/hbase/util/Bytes.java | 11 ++--------- .../java/org/apache/hadoop/hbase/util/TestBytes.java | 8 ++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) 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()); + } + } }