Author: orudyy
Date: Mon Nov 21 11:07:40 2016
New Revision: 1770644

URL: http://svn.apache.org/viewvc?rev=1770644&view=rev
Log:
QPID-7534: EncodingUtils#readLongAsShortString should guard against the string 
containing characters not in the range '0'-'9'

Added:
    
qpid/java/trunk/common/src/test/java/org/apache/qpid/framing/EncodingUtilsTest.java
Modified:
    
qpid/java/trunk/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java

Modified: 
qpid/java/trunk/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java?rev=1770644&r1=1770643&r2=1770644&view=diff
==============================================================================
--- 
qpid/java/trunk/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java 
(original)
+++ 
qpid/java/trunk/common/src/main/java/org/apache/qpid/framing/EncodingUtils.java 
Mon Nov 21 11:07:40 2016
@@ -318,7 +318,7 @@ public class EncodingUtils
         return encodedByteLength();
     }
 
-    public static long readLongAsShortString(QpidByteBuffer buffer)
+    public static long readLongAsShortString(QpidByteBuffer buffer) throws 
AMQFrameDecodingException
     {
         short length = buffer.getUnsignedByte();
         short pos = 0;
@@ -341,7 +341,7 @@ public class EncodingUtils
             isNegative = false;
         }
 
-        result = digit - (byte) '0';
+        result = toNumber(digit);
         pos++;
 
         while (pos < length)
@@ -349,12 +349,22 @@ public class EncodingUtils
             pos++;
             digit = buffer.get();
             result = (result << 3) + (result << 1);
-            result += digit - (byte) '0';
+            result += toNumber(digit);
         }
 
         return isNegative ? -result : result;
     }
 
+    private static int toNumber(final byte digit) throws 
AMQFrameDecodingException
+    {
+        if (digit >= '0' && digit <= '9')
+        {
+            return digit - (byte) '0';
+        }
+        throw new AMQFrameDecodingException(String.format("Unexpected 
character '%c' in string representing long value",
+                                                          digit));
+    }
+
     public static byte[] asUTF8Bytes(CharSequence string)
     {
         byte[] bytes = new byte[getUTF8Length(string)];

Added: 
qpid/java/trunk/common/src/test/java/org/apache/qpid/framing/EncodingUtilsTest.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/common/src/test/java/org/apache/qpid/framing/EncodingUtilsTest.java?rev=1770644&view=auto
==============================================================================
--- 
qpid/java/trunk/common/src/test/java/org/apache/qpid/framing/EncodingUtilsTest.java
 (added)
+++ 
qpid/java/trunk/common/src/test/java/org/apache/qpid/framing/EncodingUtilsTest.java
 Mon Nov 21 11:07:40 2016
@@ -0,0 +1,81 @@
+/*
+ * 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.qpid.framing;
+
+import org.apache.qpid.bytebuffer.QpidByteBuffer;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+
+public class EncodingUtilsTest extends QpidTestCase
+{
+    private static final int BUFFER_SIZE = 10;
+    private static final int POOL_SIZE = 20;
+
+    private QpidByteBuffer _buffer;
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        QpidByteBuffer.initialisePool(BUFFER_SIZE, POOL_SIZE);
+        _buffer = QpidByteBuffer.allocateDirect(BUFFER_SIZE);
+    }
+
+    @Override
+    public void tearDown() throws Exception
+    {
+        try
+        {
+            _buffer.dispose();
+        }
+        finally
+        {
+            super.tearDown();
+        }
+    }
+
+    public void testReadLongAsShortStringWhenDigitsAreSpecified() throws 
Exception
+    {
+        _buffer.putUnsignedByte((short)3);
+        _buffer.put((byte)'9');
+        _buffer.put((byte)'2');
+        _buffer.put((byte)'0');
+        _buffer.flip();
+        assertEquals("Unexpected result", 920L, 
EncodingUtils.readLongAsShortString(_buffer));
+    }
+
+    public void testReadLongAsShortStringWhenNonDigitCharacterIsSpecified() 
throws Exception
+    {
+        _buffer.putUnsignedByte((short)2);
+        _buffer.put((byte)'1');
+        _buffer.put((byte)'a');
+        _buffer.flip();
+        try
+        {
+            EncodingUtils.readLongAsShortString(_buffer);
+            fail("Exception is expected");
+        }
+        catch(AMQFrameDecodingException e)
+        {
+            // pass
+        }
+    }
+}
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to