Fixed bug reading string where null characters were included within content

This commit is contained in:
Collin Smith 2020-08-18 01:57:18 -07:00
parent 7667cfdff2
commit 3c56558e9c
2 changed files with 6 additions and 3 deletions

View File

@ -495,9 +495,9 @@ public class BitInput {
final byte[] dst = new byte[maxLen];
for (int i = 0; i < maxLen; i++) {
final byte b = dst[i] = (byte) readUnsigned(bits);
if (nullTerminated && b == '\0') break;
if (nullTerminated && b == '\0') maxLen = i;
}
return new String(dst);
return new String(dst, 0, maxLen);
}
}

View File

@ -5,6 +5,7 @@ import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;
import com.riiablo.util.DebugUtils;
@ -487,7 +488,9 @@ public class ByteInput {
assert aligned() : "not aligned";
try {
incrementBitsRead((long) len * Byte.SIZE);
return buffer.readCharSequence(len, CharsetUtil.US_ASCII).toString();
CharSequence charSequence = buffer.readCharSequence(len, CharsetUtil.US_ASCII);
charSequence = charSequence.subSequence(0, StringUtils.indexOf(charSequence, '\0'));
return charSequence.toString();
} catch (IndexOutOfBoundsException t) {
throw new EndOfInput(t);
}