From fcbd346d8f4a52b13857ff535c108762f86c1ab9 Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 7 Aug 2024 07:56:33 +0200 Subject: [PATCH] fix: do not StackOverflow when getting a section in FULL after awkward trim (#2863) --- .../implementation/blocks/CharBlocks.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/blocks/CharBlocks.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/blocks/CharBlocks.java index 047394322..c338033f0 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/blocks/CharBlocks.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/queue/implementation/blocks/CharBlocks.java @@ -31,7 +31,9 @@ public abstract class CharBlocks implements IBlocks { char[] arr = blocks.blocks[layer]; if (arr == null) { // Chunk probably trimmed mid-operations, but do nothing about it to avoid other issues - return EMPTY.get(blocks, layer, false); + synchronized (blocks.sectionLocks[layer]) { + return getSkipFull(blocks, layer, aggressive); + } } return arr; } @@ -54,22 +56,7 @@ public abstract class CharBlocks implements IBlocks { if (blocks.sections[layer] == FULL) { return FULL.get(blocks, layer); } - char[] arr = blocks.blocks[layer]; - if (arr == null) { - arr = blocks.blocks[layer] = blocks.update(layer, null, aggressive); - if (arr == null) { - throw new IllegalStateException("Array cannot be null: " + blocks.getClass()); - } - } else { - blocks.blocks[layer] = blocks.update(layer, arr, aggressive); - if (blocks.blocks[layer] == null) { - throw new IllegalStateException("Array cannot be null (update): " + blocks.getClass()); - } - } - if (blocks.blocks[layer] != null) { - blocks.sections[layer] = FULL; - } - return arr; + return getSkipFull(blocks, layer, aggressive); } } @@ -262,6 +249,25 @@ public abstract class CharBlocks implements IBlocks { get(blocks, layer)[index] = value; } + static char[] getSkipFull(CharBlocks blocks, int layer, boolean aggressive) { + char[] arr = blocks.blocks[layer]; + if (arr == null) { + arr = blocks.blocks[layer] = blocks.update(layer, null, aggressive); + if (arr == null) { + throw new IllegalStateException("Array cannot be null: " + blocks.getClass()); + } + } else { + blocks.blocks[layer] = blocks.update(layer, arr, aggressive); + if (blocks.blocks[layer] == null) { + throw new IllegalStateException("Array cannot be null (update): " + blocks.getClass()); + } + } + if (blocks.blocks[layer] != null) { + blocks.sections[layer] = FULL; + } + return arr; + } + } }