diff options
author | Thomas Wolf <thomas.wolf@paranor.ch> | 2021-01-22 22:45:48 +0100 |
---|---|---|
committer | Thomas Wolf <thomas.wolf@paranor.ch> | 2021-01-22 23:00:01 +0100 |
commit | 84dbc2d43169cdd41c79d853b75483cfd76ce7d6 (patch) | |
tree | 15eee2b5d374b9cbb4341c5a85221cffdf4a8e7b /org.eclipse.jgit.test/tst | |
parent | cf9433a9b3c3359fbb4cbdd9c65b99b44eba08e8 (diff) | |
download | jgit-84dbc2d43169cdd41c79d853b75483cfd76ce7d6.tar.gz jgit-84dbc2d43169cdd41c79d853b75483cfd76ce7d6.zip |
TemporaryBuffer: fix toByteArray(limit)
Heap always copied whole blocks, which leads to AIOOBEs. LocalFile
didn't overwrite the method and thus caused NPEs.
Change-Id: Ia37d4a875df9f25d4825e6bc95fed7f0dff42afb
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.jgit.test/tst')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java index 4e65ca7a4b..01dcde29bd 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/TemporaryBufferTest.java @@ -406,4 +406,69 @@ public class TemporaryBufferTest { } } } + + @Test + public void testHeapToByteArrayWithLimit() throws IOException { + int sz = 2 * Block.SZ; + try (TemporaryBuffer b = new TemporaryBuffer.Heap(sz / 2, sz)) { + for (int i = 0; i < sz; i++) { + b.write('a' + i % 26); + } + byte[] prefix = b.toByteArray(5); + assertEquals(5, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(Block.SZ + 37); + assertEquals(Block.SZ + 37, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz + 37); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + } + } + + @Test + public void testFileToByteArrayWithLimit() throws IOException { + @SuppressWarnings("resource") // Buffer is explicitly destroyed in finally block + TemporaryBuffer b = new TemporaryBuffer.LocalFile(null, 2 * Block.SZ); + int sz = 3 * Block.SZ; + try { + for (int i = 0; i < sz; i++) { + b.write('a' + i % 26); + } + b.close(); + byte[] prefix = b.toByteArray(5); + assertEquals(5, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(Block.SZ + 37); + assertEquals(Block.SZ + 37, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + prefix = b.toByteArray(sz + 37); + assertEquals(sz, prefix.length); + for (int i = 0; i < prefix.length; i++) { + assertEquals('a' + i % 26, prefix[i]); + } + } finally { + b.destroy(); + } + } } |