Skip to content

Commit

Permalink
Fix MemoryMount using incorrect file lengths
Browse files Browse the repository at this point in the history
 - We checked the backing array when reading rather than the file's
   length, so could read beyond the end of the file.
 - We used the entry length when resizing, which effectively meant we
   doubled the size of the backing array on each write.
  • Loading branch information
SquidDev committed Feb 7, 2024
1 parent 8db5c6b commit f14cb2a
Showing 1 changed file with 3 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,17 @@ public int read(ByteBuffer destination) throws IOException {
checkClosed();

var backing = Nullability.assertNonNull(entry.contents);
if (position >= backing.length) return -1;
if (position >= entry.length) return -1;

var remaining = Math.min(backing.length - (int) position, destination.remaining());
var remaining = Math.min(entry.length - (int) position, destination.remaining());
destination.put(backing, (int) position, remaining);
position += remaining;
return remaining;
}

private byte[] ensureCapacity(int capacity) {
var contents = Nullability.assertNonNull(entry.contents);
if (capacity >= entry.length) {
if (capacity >= contents.length) {
var newCapacity = Math.max(capacity, contents.length << 1);
contents = entry.contents = Arrays.copyOf(contents, newCapacity);
}
Expand Down

0 comments on commit f14cb2a

Please sign in to comment.