mojira.dev
MC-313113

Game crashes when copying too much text

When copying too much text the game will crash due to an out-of-memory error thrown by LWJGL’s MemoryStack, this is because of the limited stack size used by LWJGL for the clipboard methods. This same issue was also present in the GLFW implementation, but the game had not used the string variant of the methods, which got around this stack size limitation.

I’ve attached a datapack that will load a dialog. If you copy the text within, paste it back into the box so that it's twice the size, and copy it again, your game will crash.

The code:
This is all contained within com.mojang.blaze3d.platform.ClipboardManager
In 26.3, the code looks like this:

public void setClipboard(String clipboard) {
    if (!SDLClipboard.SDL_SetClipboardText(clipboard)) {
        LOGGER.error("Failed to set clipboard: {}", SDLError.SDL_GetError());
    }
}

In 26.2, it looks like this

private static void pushClipboard(Window window, ByteBuffer buffer, byte[] data) {
    buffer.clear();
    buffer.put(data);
    buffer.put((byte)0);
    buffer.flip();
    GLFW.glfwSetClipboardString(window.handle(), buffer);
}

public void setClipboard(Window window, String clipboard) {
    byte[] encoded = clipboard.getBytes(StandardCharsets.UTF_8);
    int encodedLength = encoded.length + 1;
    if (encodedLength < this.clipboardScratchBuffer.capacity()) {
        pushClipboard(window, this.clipboardScratchBuffer, encoded);
    } else {
        ByteBuffer buffer = MemoryUtil.memAlloc(encodedLength);

        try {
            pushClipboard(window, buffer, encoded);
        } finally {
            MemoryUtil.memFree(buffer);
        }
    }
}

As seen, 26.2 is more involved and allocates its own memory, which is why before 26.3 the game did not crash in these situations as the memory was not limited.

Attachments

Comments 1

Thank you for helping us improve Minecraft! We saved your files:

[media][media]

ThatGravyBoat

(Unassigned)

Confirmed

26.3, 26.4 Snapshot 7

Retrieved