Fix CharSequenceValueConverter.convertToByte implementation for AsciiString (#7994)

Motivation:

The implementation of CharSequenceValueConverter.convertToByte did not correctly handle AsciiString if the length != 1.

Modifications:

- Only use fast-path for AsciiString with length of 1.
- Add unit tests.

Result:

Fixes https://github.com/netty/netty/issues/7990
This commit is contained in:
Norman Maurer 2018-06-01 21:15:08 +02:00 committed by GitHub
parent a4393831f0
commit 1611acf4ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -77,7 +77,7 @@ public class CharSequenceValueConverter implements ValueConverter<CharSequence>
@Override
public byte convertToByte(CharSequence value) {
if (value instanceof AsciiString) {
if (value instanceof AsciiString && value.length() == 1) {
return ((AsciiString) value).byteAt(0);
}
return Byte.parseByte(value.toString());

View File

@ -14,6 +14,7 @@
*/
package io.netty.handler.codec;
import io.netty.util.AsciiString;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -30,6 +31,16 @@ public class CharSequenceValueConverterTest {
assertFalse(converter.convertToBoolean(converter.convertBoolean(false)));
}
@Test
public void testByteFromAsciiString() {
assertEquals(127, converter.convertToByte(AsciiString.of("127")));
}
@Test(expected = NumberFormatException.class)
public void testByteFromEmptyAsciiString() {
converter.convertToByte(AsciiString.EMPTY_STRING);
}
@Test
public void testByte() {
assertEquals(Byte.MAX_VALUE, converter.convertToByte(converter.convertByte(Byte.MAX_VALUE)));