From f14cb2a3d1edd113cc739de4bc740433934cd9ee Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 7 Feb 2024 21:36:17 +0000 Subject: [PATCH] Fix MemoryMount using incorrect file lengths - 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. --- .../dan200/computercraft/core/filesystem/MemoryMount.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/projects/core/src/main/java/dan200/computercraft/core/filesystem/MemoryMount.java b/projects/core/src/main/java/dan200/computercraft/core/filesystem/MemoryMount.java index c8e09a5863..2db0154b6f 100644 --- a/projects/core/src/main/java/dan200/computercraft/core/filesystem/MemoryMount.java +++ b/projects/core/src/main/java/dan200/computercraft/core/filesystem/MemoryMount.java @@ -275,9 +275,9 @@ 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; @@ -285,7 +285,7 @@ public int read(ByteBuffer destination) throws IOException { 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); }