Return-Path: X-Original-To: apmail-cassandra-commits-archive@www.apache.org Delivered-To: apmail-cassandra-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A15CE9B69 for ; Tue, 27 Sep 2011 20:13:57 +0000 (UTC) Received: (qmail 98487 invoked by uid 500); 27 Sep 2011 20:13:57 -0000 Delivered-To: apmail-cassandra-commits-archive@cassandra.apache.org Received: (qmail 98461 invoked by uid 500); 27 Sep 2011 20:13:57 -0000 Mailing-List: contact commits-help@cassandra.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cassandra.apache.org Delivered-To: mailing list commits@cassandra.apache.org Received: (qmail 98452 invoked by uid 99); 27 Sep 2011 20:13:57 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Sep 2011 20:13:57 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Sep 2011 20:13:54 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id D8D422388A6C for ; Tue, 27 Sep 2011 20:13:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1176589 - in /cassandra/branches/cassandra-0.8: CHANGES.txt src/java/org/apache/cassandra/utils/FBUtilities.java test/unit/org/apache/cassandra/db/marshal/BytesTypeTest.java Date: Tue, 27 Sep 2011 20:13:32 -0000 To: commits@cassandra.apache.org From: xedin@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20110927201332.D8D422388A6C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: xedin Date: Tue Sep 27 20:13:32 2011 New Revision: 1176589 URL: http://svn.apache.org/viewvc?rev=1176589&view=rev Log: FBUtilities.hexToBytes(String) to throw NumberFormatException when string contains non-hex characters patch by Jonathan Ellis and Pavel Yaskevich; reviewed by Pavel Yaskevich for (CASSANDRA-3231) Added: cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/db/marshal/BytesTypeTest.java Modified: cassandra/branches/cassandra-0.8/CHANGES.txt cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/utils/FBUtilities.java Modified: cassandra/branches/cassandra-0.8/CHANGES.txt URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/CHANGES.txt?rev=1176589&r1=1176588&r2=1176589&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/CHANGES.txt (original) +++ cassandra/branches/cassandra-0.8/CHANGES.txt Tue Sep 27 20:13:32 2011 @@ -8,7 +8,8 @@ * Log a miningfull warning when a node receive a message for a repair session that don't exist anymore (CASSANDRA-3256) * Fix FD leak when internode encryption is enabled (CASSANDRA-3257) - + * FBUtilities.hexToBytes(String) to throw NumberFormatException when string + contains non-hex characters (CASSANDRA-3231) 0.8.6 * revert CASSANDRA-2388 Modified: cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/utils/FBUtilities.java URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/utils/FBUtilities.java?rev=1176589&r1=1176588&r2=1176589&view=diff ============================================================================== --- cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/utils/FBUtilities.java (original) +++ cassandra/branches/cassandra-0.8/src/java/org/apache/cassandra/utils/FBUtilities.java Tue Sep 27 20:13:32 2011 @@ -44,8 +44,6 @@ import org.apache.cassandra.cache.IRowCa import org.apache.cassandra.config.ConfigurationException; import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.db.DecoratedKey; -import org.apache.cassandra.db.marshal.AbstractType; -import org.apache.cassandra.db.marshal.TypeParser; import org.apache.cassandra.dht.IPartitioner; import org.apache.cassandra.dht.Range; import org.apache.cassandra.dht.Token; @@ -369,10 +367,14 @@ public class FBUtilities { if (str.length() % 2 == 1) str = "0" + str; - byte[] bytes = new byte[str.length()/2]; + byte[] bytes = new byte[str.length() / 2]; for (int i = 0; i < bytes.length; i++) { - bytes[i] = (byte)((charToByte[str.charAt(i * 2)] << 4) | charToByte[str.charAt(i*2 + 1)]); + byte halfByte1 = charToByte[str.charAt(i * 2)]; + byte halfByte2 = charToByte[str.charAt(i * 2 + 1)]; + if (halfByte1 == -1 || halfByte2 == -1) + throw new NumberFormatException("Non-hex characters in " + str); + bytes[i] = (byte)((halfByte1 << 4) | halfByte2); } return bytes; } Added: cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/db/marshal/BytesTypeTest.java URL: http://svn.apache.org/viewvc/cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/db/marshal/BytesTypeTest.java?rev=1176589&view=auto ============================================================================== --- cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/db/marshal/BytesTypeTest.java (added) +++ cassandra/branches/cassandra-0.8/test/unit/org/apache/cassandra/db/marshal/BytesTypeTest.java Tue Sep 27 20:13:32 2011 @@ -0,0 +1,40 @@ +/** + * 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.cassandra.db.marshal; + +import org.junit.Test; + +public class BytesTypeTest +{ + private static final String INVALID_HEX = "33AG45F"; // Invalid (has a G) + private static final String VALID_HEX = "33A45F"; + + @Test (expected = MarshalException.class) + public void testFromStringWithInvalidString() + { + BytesType.instance.fromString(INVALID_HEX); + } + + @Test + public void testFromStringWithValidString() + { + BytesType.instance.fromString(VALID_HEX); + } +}