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
This commit is contained in:
Michael Stack 2012-09-12 19:51:19 +00:00
parent e153a0c9ac
commit 0c13da349b
2 changed files with 10 additions and 9 deletions

View File

@ -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);

View File

@ -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());
}
}
}