Skip to content

Commit

Permalink
Fix not thread safe static buffer usage
Browse files Browse the repository at this point in the history
DefaultExtractorInput.SCRATCH_SPACE buffer is used to skip data by
reading it into this buffer and discarding.

Simultaneous use of skip methods corrupts this buffer. Normally the
read data is discarded so it doesn't matter but the underlying
DataSource may use the buffer too. If it's a CacheDataSource it uses
this buffer to read data from upstream then write to cache.

Issue: #3762

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=184502170
  • Loading branch information
erdemguven authored and andrewlewis committed Feb 8, 2018
1 parent e437248 commit de293af
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public final class DefaultExtractorInput implements ExtractorInput {

private static final int PEEK_MIN_FREE_SPACE_AFTER_RESIZE = 64 * 1024;
private static final int PEEK_MAX_FREE_SPACE = 512 * 1024;
private static final byte[] SCRATCH_SPACE = new byte[4096];
private static final int SCRATCH_SPACE_SIZE = 4096;

private final byte[] scratchSpace;
private final DataSource dataSource;
private final long streamLength;

Expand All @@ -50,6 +51,7 @@ public DefaultExtractorInput(DataSource dataSource, long position, long length)
this.position = position;
this.streamLength = length;
peekBuffer = new byte[PEEK_MIN_FREE_SPACE_AFTER_RESIZE];
scratchSpace = new byte[SCRATCH_SPACE_SIZE];
}

@Override
Expand Down Expand Up @@ -84,7 +86,7 @@ public int skip(int length) throws IOException, InterruptedException {
int bytesSkipped = skipFromPeekBuffer(length);
if (bytesSkipped == 0) {
bytesSkipped =
readFromDataSource(SCRATCH_SPACE, 0, Math.min(length, SCRATCH_SPACE.length), 0, true);
readFromDataSource(scratchSpace, 0, Math.min(length, scratchSpace.length), 0, true);
}
commitBytesRead(bytesSkipped);
return bytesSkipped;
Expand All @@ -95,8 +97,9 @@ public boolean skipFully(int length, boolean allowEndOfInput)
throws IOException, InterruptedException {
int bytesSkipped = skipFromPeekBuffer(length);
while (bytesSkipped < length && bytesSkipped != C.RESULT_END_OF_INPUT) {
bytesSkipped = readFromDataSource(SCRATCH_SPACE, -bytesSkipped,
Math.min(length, bytesSkipped + SCRATCH_SPACE.length), bytesSkipped, allowEndOfInput);
int minLength = Math.min(length, bytesSkipped + scratchSpace.length);
bytesSkipped =
readFromDataSource(scratchSpace, -bytesSkipped, minLength, bytesSkipped, allowEndOfInput);
}
commitBytesRead(bytesSkipped);
return bytesSkipped != C.RESULT_END_OF_INPUT;
Expand Down

0 comments on commit de293af

Please sign in to comment.