mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Prevent ParsableByteArray.bytesLeft()
returning a negative value
PiperOrigin-RevId: 735417945
This commit is contained in:
parent
a110b02142
commit
e8842b939c
@ -142,7 +142,7 @@ public final class ParsableByteArray {
|
||||
|
||||
/** Returns the number of bytes yet to be read. */
|
||||
public int bytesLeft() {
|
||||
return limit - position;
|
||||
return Math.max(limit - position, 0);
|
||||
}
|
||||
|
||||
/** Returns the limit. */
|
||||
@ -261,7 +261,7 @@ public final class ParsableByteArray {
|
||||
public char peekChar(Charset charset) {
|
||||
Assertions.checkArgument(
|
||||
SUPPORTED_CHARSETS_FOR_READLINE.contains(charset), "Unsupported charset: " + charset);
|
||||
if (bytesLeft() < 1) {
|
||||
if (bytesLeft() == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (charset.equals(StandardCharsets.US_ASCII)) {
|
||||
|
@ -3061,7 +3061,7 @@ public final class Util {
|
||||
@UnstableApi
|
||||
public static boolean inflate(
|
||||
ParsableByteArray input, ParsableByteArray output, @Nullable Inflater inflater) {
|
||||
if (input.bytesLeft() <= 0) {
|
||||
if (input.bytesLeft() == 0) {
|
||||
return false;
|
||||
}
|
||||
if (output.capacity() < input.bytesLeft()) {
|
||||
|
@ -77,6 +77,26 @@ public final class ParsableByteArrayTest {
|
||||
assertThat(array.limit()).isEqualTo(4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bytesLeft() {
|
||||
ParsableByteArray array = getTestDataArray();
|
||||
assertThat(array.bytesLeft()).isEqualTo(TEST_DATA.length);
|
||||
|
||||
array.setPosition(1);
|
||||
array.setLimit(2);
|
||||
assertThat(array.bytesLeft()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bytesLeft_positionExceedsLimit_returnsZero() {
|
||||
ParsableByteArray array = getTestDataArray();
|
||||
array.setLimit(1);
|
||||
// readInt advances position without checking limit (see b/147657250)
|
||||
int unused = array.readInt();
|
||||
|
||||
assertThat(array.bytesLeft()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readShort() {
|
||||
testReadShort((short) -1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user