mojira.dev
MC-269744

Out of memory crash when using a preset with a large layer

By simply including a layer far beyond what's reasonable in a superflat preset, the game can crash due to an OutOfMemoryError. Here's a silly preset that produces this issue:

minecraft:dirt,2147483647*minecraft:bedrock;minecraft:plains
minecraft:dirt,2147483647*minecraft:bedrock;minecraft:plains

Upon clicking the 'Use Preset' button, the game crashes. This preset is very silly, and I doubt anyone with sense would go out of their way to write a similar one, but I'd at least expect the game not to crash.

Code analysis (Yarn mappings)

The stack trace is:

Reported exception thrown!
net.minecraft.util.crash.CrashException: mouseClicked event handler
	at net.minecraft.client.gui.screen.Screen.wrapScreenError(Screen.java:419) ~[client-intermediary.jar:?]
	at net.minecraft.client.Mouse.onMouseButton(Mouse.java:99) ~[client-intermediary.jar:?]
	at net.minecraft.client.Mouse.method_22686(Mouse.java:180) ~[client-intermediary.jar:?]
	at net.minecraft.util.thread.ThreadExecutor.execute(ThreadExecutor.java:102) ~[client-intermediary.jar:?]
	at net.minecraft.client.Mouse.method_22684(Mouse.java:180) ~[client-intermediary.jar:?]
Caused by: java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3482) ~[?:?]
	at java.util.ArrayList.grow(ArrayList.java:237) ~[?:?]
	at java.util.ArrayList.grow(ArrayList.java:244) ~[?:?]
	at java.util.ArrayList.add(ArrayList.java:483) ~[?:?]
	at java.util.ArrayList.add(ArrayList.java:496) ~[?:?]
	at net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig.updateLayerBlocks(FlatChunkGeneratorConfig.java:184) ~[client-intermediary.jar:?]
	at net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig.with(FlatChunkGeneratorConfig.java:98) ~[client-intermediary.jar:?]
	at net.minecraft.client.gui.screen.world.PresetsScreen.parsePresetString(PresetsScreen.java:152) ~[client-intermediary.jar:?]
	at net.minecraft.client.gui.screen.world.PresetsScreen.method_19847(PresetsScreen.java:193) ~[client-intermediary.jar:?]
Reported exception thrown!
net.minecraft.util.crash.CrashException: mouseClicked event handler
	at net.minecraft.client.gui.screen.Screen.wrapScreenError(Screen.java:419) ~[client-intermediary.jar:?]
	at net.minecraft.client.Mouse.onMouseButton(Mouse.java:99) ~[client-intermediary.jar:?]
	at net.minecraft.client.Mouse.method_22686(Mouse.java:180) ~[client-intermediary.jar:?]
	at net.minecraft.util.thread.ThreadExecutor.execute(ThreadExecutor.java:102) ~[client-intermediary.jar:?]
	at net.minecraft.client.Mouse.method_22684(Mouse.java:180) ~[client-intermediary.jar:?]
Caused by: java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3482) ~[?:?]
	at java.util.ArrayList.grow(ArrayList.java:237) ~[?:?]
	at java.util.ArrayList.grow(ArrayList.java:244) ~[?:?]
	at java.util.ArrayList.add(ArrayList.java:483) ~[?:?]
	at java.util.ArrayList.add(ArrayList.java:496) ~[?:?]
	at net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig.updateLayerBlocks(FlatChunkGeneratorConfig.java:184) ~[client-intermediary.jar:?]
	at net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig.with(FlatChunkGeneratorConfig.java:98) ~[client-intermediary.jar:?]
	at net.minecraft.client.gui.screen.world.PresetsScreen.parsePresetString(PresetsScreen.java:152) ~[client-intermediary.jar:?]
	at net.minecraft.client.gui.screen.world.PresetsScreen.method_19847(PresetsScreen.java:193) ~[client-intermediary.jar:?]

The preset code string is parsed into a FlatChunkGeneratorConfig instance. During the creation of this FlatChunkGeneratorConfig, each layer is processed to create a list of block states in the world. A size 2,147,483,648 list, understandably, cannot be stored in an average allocation of memory, so the game crashes.

My proposed fix is applying the dimension maximum height maximum restriction to the layers in a way that prevents overflows when considering the current layer start height, as fundamentally this issue seems to be caused by integer overflow. This validation occurs within the PresetsScreen#parseLayerString method.

Attachments

Comments 0

No comments.

haykam

Panda4994

Confirmed

Platform

Normal

Crash

1.20.4, 24w12a, 24w14a, 1.20.5, 1.21 Pre-Release 1, 25w02a

25w20a

Retrieved