Format CodedInputStream & Test

PiperOrigin-RevId: 599528929
pull/15484/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 2c28d728da
commit 979c39178b
  1. 23
      java/core/src/main/java/com/google/protobuf/CodedInputStream.java
  2. 7
      java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java

@ -177,6 +177,7 @@ public abstract class CodedInputStream {
throw InvalidProtocolBufferException.recursionLimitExceeded(); throw InvalidProtocolBufferException.recursionLimitExceeded();
} }
} }
/** Disable construction/inheritance outside of this class. */ /** Disable construction/inheritance outside of this class. */
private CodedInputStream() {} private CodedInputStream() {}
@ -2005,6 +2006,7 @@ public abstract class CodedInputStream {
private static final class StreamDecoder extends CodedInputStream { private static final class StreamDecoder extends CodedInputStream {
private final InputStream input; private final InputStream input;
private final byte[] buffer; private final byte[] buffer;
/** bufferSize represents how many bytes are currently filled in the buffer */ /** bufferSize represents how many bytes are currently filled in the buffer */
private int bufferSize; private int bufferSize;
@ -2842,11 +2844,11 @@ public abstract class CodedInputStream {
* Exactly like readRawBytes, but caller must have already checked the fast path: (size <= * Exactly like readRawBytes, but caller must have already checked the fast path: (size <=
* (bufferSize - pos) && size > 0) * (bufferSize - pos) && size > 0)
* *
* If ensureNoLeakedReferences is true, the value is guaranteed to have not escaped to * <p>If ensureNoLeakedReferences is true, the value is guaranteed to have not escaped to
* untrusted code. * untrusted code.
*/ */
private byte[] readRawBytesSlowPath( private byte[] readRawBytesSlowPath(final int size, boolean ensureNoLeakedReferences)
final int size, boolean ensureNoLeakedReferences) throws IOException { throws IOException {
// Attempt to read the data in one byte array when it's safe to do. // Attempt to read the data in one byte array when it's safe to do.
byte[] result = readRawBytesSlowPathOneChunk(size); byte[] result = readRawBytesSlowPathOneChunk(size);
if (result != null) { if (result != null) {
@ -2947,7 +2949,7 @@ public abstract class CodedInputStream {
/** /**
* Reads the remaining data in small chunks from the input stream. * Reads the remaining data in small chunks from the input stream.
* *
* Returns a byte[] that may have escaped to user code via InputStream APIs. * <p>Returns a byte[] that may have escaped to user code via InputStream APIs.
*/ */
private List<byte[]> readRawBytesSlowPathRemainingChunks(int sizeLeft) throws IOException { private List<byte[]> readRawBytesSlowPathRemainingChunks(int sizeLeft) throws IOException {
// The size is very large. For security reasons, we can't allocate the // The size is very large. For security reasons, we can't allocate the
@ -3105,41 +3107,54 @@ public abstract class CodedInputStream {
private static final class IterableDirectByteBufferDecoder extends CodedInputStream { private static final class IterableDirectByteBufferDecoder extends CodedInputStream {
/** The object that need to decode. */ /** The object that need to decode. */
private final Iterable<ByteBuffer> input; private final Iterable<ByteBuffer> input;
/** The {@link Iterator} with type {@link ByteBuffer} of {@code input} */ /** The {@link Iterator} with type {@link ByteBuffer} of {@code input} */
private final Iterator<ByteBuffer> iterator; private final Iterator<ByteBuffer> iterator;
/** The current ByteBuffer; */ /** The current ByteBuffer; */
private ByteBuffer currentByteBuffer; private ByteBuffer currentByteBuffer;
/** /**
* If {@code true}, indicates that all the buffers are backing a {@link ByteString} and are * If {@code true}, indicates that all the buffers are backing a {@link ByteString} and are
* therefore considered to be an immutable input source. * therefore considered to be an immutable input source.
*/ */
private final boolean immutable; private final boolean immutable;
/** /**
* If {@code true}, indicates that calls to read {@link ByteString} or {@code byte[]} * If {@code true}, indicates that calls to read {@link ByteString} or {@code byte[]}
* <strong>may</strong> return slices of the underlying buffer, rather than copies. * <strong>may</strong> return slices of the underlying buffer, rather than copies.
*/ */
private boolean enableAliasing; private boolean enableAliasing;
/** The global total message length limit */ /** The global total message length limit */
private int totalBufferSize; private int totalBufferSize;
/** The amount of available data in the input beyond {@link #currentLimit}. */ /** The amount of available data in the input beyond {@link #currentLimit}. */
private int bufferSizeAfterCurrentLimit; private int bufferSizeAfterCurrentLimit;
/** The absolute position of the end of the current message. */ /** The absolute position of the end of the current message. */
private int currentLimit = Integer.MAX_VALUE; private int currentLimit = Integer.MAX_VALUE;
/** The last tag that was read from this stream. */ /** The last tag that was read from this stream. */
private int lastTag; private int lastTag;
/** Total Bytes have been Read from the {@link Iterable} {@link ByteBuffer} */ /** Total Bytes have been Read from the {@link Iterable} {@link ByteBuffer} */
private int totalBytesRead; private int totalBytesRead;
/** The start position offset of the whole message, used as to reset the totalBytesRead */ /** The start position offset of the whole message, used as to reset the totalBytesRead */
private int startOffset; private int startOffset;
/** The current position for current ByteBuffer */ /** The current position for current ByteBuffer */
private long currentByteBufferPos; private long currentByteBufferPos;
private long currentByteBufferStartPos; private long currentByteBufferStartPos;
/** /**
* If the current ByteBuffer is unsafe-direct based, currentAddress is the start address of this * If the current ByteBuffer is unsafe-direct based, currentAddress is the start address of this
* ByteBuffer; otherwise should be zero. * ByteBuffer; otherwise should be zero.
*/ */
private long currentAddress; private long currentAddress;
/** The limit position for current ByteBuffer */ /** The limit position for current ByteBuffer */
private long currentByteBufferLimit; private long currentByteBufferLimit;

@ -472,7 +472,7 @@ public class CodedInputStreamTest {
/** Skipping a huge blob should not allocate excessive memory, so there should be no limit */ /** Skipping a huge blob should not allocate excessive memory, so there should be no limit */
@Test @Test
public void testSkipMaliciouslyHugeBlob() throws Exception { public void testSkipMaliciouslyHugeBlob() throws Exception {
InputStream is = new RepeatingInputStream(new byte[]{1}, Integer.MAX_VALUE); InputStream is = new RepeatingInputStream(new byte[] {1}, Integer.MAX_VALUE);
CodedInputStream.newInstance(is).skipRawBytes(Integer.MAX_VALUE); CodedInputStream.newInstance(is).skipRawBytes(Integer.MAX_VALUE);
} }
@ -1263,10 +1263,11 @@ public class CodedInputStreamTest {
public void testMaliciousInputStream() throws Exception { public void testMaliciousInputStream() throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream); CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream);
codedOutputStream.writeByteArrayNoTag(new byte[] { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }); codedOutputStream.writeByteArrayNoTag(new byte[] {0x0, 0x1, 0x2, 0x3, 0x4, 0x5});
codedOutputStream.flush(); codedOutputStream.flush();
final List<byte[]> maliciousCapture = new ArrayList<>(); final List<byte[]> maliciousCapture = new ArrayList<>();
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()) { InputStream inputStream =
new ByteArrayInputStream(outputStream.toByteArray()) {
@Override @Override
public synchronized int read(byte[] b, int off, int len) { public synchronized int read(byte[] b, int off, int len) {
maliciousCapture.add(b); maliciousCapture.add(b);

Loading…
Cancel
Save