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);
+    }
+}


Reply via email to