mirror of https://github.com/apache/cassandra
validate UTF8 keys in legacy OPP and COPP. patch by Nick Bailey; reviewed by Stu Hood and jbellis
git-svn-id: https://svn.apache.org/repos/asf/cassandra/trunk@959483 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5ab5e1bbd1
commit
9c0ad8147d
|
|
@ -19,6 +19,7 @@
|
|||
package org.apache.cassandra.dht;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.text.Collator;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
|
@ -39,7 +40,16 @@ public class CollatingOrderPreservingPartitioner extends AbstractByteOrderedPart
|
|||
{
|
||||
if (key.length == 0)
|
||||
return MINIMUM;
|
||||
String skey = new String(key, FBUtilities.UTF8);
|
||||
|
||||
String skey;
|
||||
try
|
||||
{
|
||||
skey = FBUtilities.decodeToUTF8(key);
|
||||
}
|
||||
catch (CharacterCodingException e)
|
||||
{
|
||||
throw new RuntimeException("The provided key was not UTF8 encoded.", e);
|
||||
}
|
||||
return new BytesToken(collator.getCollationKey(skey).toByteArray());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ package org.apache.cassandra.dht;
|
|||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.Random;
|
||||
|
|
@ -163,6 +164,15 @@ public class OrderPreservingPartitioner implements IPartitioner<StringToken>
|
|||
|
||||
public StringToken getToken(byte[] key)
|
||||
{
|
||||
return new StringToken(new String(key, FBUtilities.UTF8));
|
||||
String skey;
|
||||
try
|
||||
{
|
||||
skey = FBUtilities.decodeToUTF8(key);
|
||||
}
|
||||
catch (CharacterCodingException e)
|
||||
{
|
||||
throw new RuntimeException("The provided key was not UTF8 encoded.", e);
|
||||
}
|
||||
return new StringToken(skey);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,9 @@ import java.net.InetAddress;
|
|||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
|
@ -37,6 +39,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import org.apache.commons.collections.iterators.CollatingIterator;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
|
@ -63,6 +66,7 @@ public class FBUtilities
|
|||
private static volatile InetAddress localInetAddress_;
|
||||
|
||||
public static final int MAX_UNSIGNED_SHORT = 0xFFFF;
|
||||
public static final CharsetDecoder utf8Decoder = Charsets.UTF_8.newDecoder();
|
||||
|
||||
public static Charset UTF8;
|
||||
static
|
||||
|
|
@ -480,6 +484,11 @@ public class FBUtilities
|
|||
return utflen;
|
||||
}
|
||||
|
||||
public static String decodeToUTF8(byte[] bytes) throws CharacterCodingException
|
||||
{
|
||||
return utf8Decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a particular bit is set using a bit mask.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ package org.apache.cassandra.utils;
|
|||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.apache.cassandra.db.IClock;
|
||||
|
|
@ -80,4 +82,11 @@ public class FBUtilitiesTest
|
|||
FBUtilities.atomicSetMax(atomicClock, new TimestampClock(3L));
|
||||
assert ((TimestampClock)atomicClock.get()).timestamp() == 9L;
|
||||
}
|
||||
|
||||
@Test(expected=CharacterCodingException.class)
|
||||
public void testDecode() throws IOException
|
||||
{
|
||||
byte[] bytes = new byte[]{(byte)0xff, (byte)0xfe};
|
||||
FBUtilities.decodeToUTF8(bytes);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue