From b8088c18b9ff99bf0a6c5341be8b99239b7fcb31 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 16 Dec 2022 12:24:56 +0100 Subject: [PATCH] fix crash in FragmentedArrayFloatList --- .../floats/FragmentedArrayFloatList.kt | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/util/collections/floats/FragmentedArrayFloatList.kt b/src/main/java/de/bixilon/minosoft/util/collections/floats/FragmentedArrayFloatList.kt index ff9c62858..08d7e6170 100644 --- a/src/main/java/de/bixilon/minosoft/util/collections/floats/FragmentedArrayFloatList.kt +++ b/src/main/java/de/bixilon/minosoft/util/collections/floats/FragmentedArrayFloatList.kt @@ -13,6 +13,7 @@ package de.bixilon.minosoft.util.collections.floats +import de.bixilon.kutil.exception.Broken import de.bixilon.minosoft.util.collections.floats.FloatListUtil.copy import org.lwjgl.system.MemoryUtil.memAllocFloat import org.lwjgl.system.MemoryUtil.memFree @@ -94,15 +95,16 @@ class FragmentedArrayFloatList( for (index in 0 until incomplete.size) { val fragment = incomplete[index + indexOffset] val remaining = fragment.limit() - fragment.position() - val copy = minOf(array.size, remaining) - offset + val copy = minOf(array.size - offset, remaining) fragment.put(array, offset, copy) offset += copy if (tryPush(fragment)) indexOffset-- - if (array.size - offset < remaining) { + if (array.size == offset) { // everything copied size += array.size + // verifyPosition() return } } @@ -113,6 +115,7 @@ class FragmentedArrayFloatList( size += length next.position(length) tryPush(next) + // verifyPosition() } override fun add(buffer: FloatBuffer) { @@ -125,14 +128,15 @@ class FragmentedArrayFloatList( for (index in 0 until incomplete.size) { val fragment = incomplete[index + indexOffset] val remaining = fragment.limit() - fragment.position() - val copy = minOf(position, remaining) - offset + val copy = minOf(position - offset, remaining) buffer.copy(offset, fragment, fragment.position(), copy) offset += copy if (tryPush(fragment)) indexOffset-- - if (position - offset < remaining) { + if (position == offset) { // everything copied size += position + // verifyPosition() return } } @@ -143,6 +147,7 @@ class FragmentedArrayFloatList( next.position(length) size += length tryPush(next) + // verifyPosition() } override fun add(floatList: AbstractFloatList) { @@ -241,7 +246,7 @@ class FragmentedArrayFloatList( forEach { it.copy(buffer) } if (buffer.position() != this.size) { // TODO: this should never happen, remove this check - throw Exception("Position mismatch: ${buffer.position()}, expected $size") + Broken("Position mismatch: ${buffer.position()}, expected $size") } this.buffer = buffer return buffer @@ -255,4 +260,13 @@ class FragmentedArrayFloatList( callable(buffer) } } + + private fun verifyPosition() { + val expected = size + var actual = 0 + forEach { actual += it.position() } + if (expected != actual) { + Broken("Buffer size mismatch: expected=$expected, actual=$actual") + } + } }