mojira.dev
MC-120572

/recipe crashes the game

The bug & how to reproduce it

If a redstone clock powers command blocks with

  1. /recipe give @a *

  2. /recipe take @a *

in them the game crashes with following error-report:

18w07c

Description: Unexpected error 

java.lang.ArithmeticException: / by zero 
at bjz.a(SourceFile:41) 
at bkc$a.a(SourceFile:115) 
at bkc.a(SourceFile:35) 
at bib.az(SourceFile:1025) 
at bib.a(SourceFile:419) 
at net.minecraft.client.main.Main.main(SourceFile:123) 
[...]

1.13-pre5

[15:21:49] [Server thread/INFO]: [@: Took 524 recipes from Gewinner413]
[15:21:49] [Server thread/INFO]: [@: Unlocked 524 recipes for Gewinner413]
[15:21:49] [Client thread/FATAL]: Unreported exception thrown!
java.lang.ArithmeticException: / by zero
	at cgu.a(SourceFile:44) ~[1.13-pre5.jar:?]
	at cgx$a.a(SourceFile:112) ~[1.13-pre5.jar:?]
	at cgx.a(SourceFile:32) ~[1.13-pre5.jar:?]
	at cep.c(SourceFile:810) ~[1.13-pre5.jar:?]
	at cep.a(SourceFile:373) [1.13-pre5.jar:?]
	at net.minecraft.client.main.Main.main(SourceFile:143) [1.13-pre5.jar:?]

The .zip file is the world that you can see on the picture with the setup. YouTube video showing the bug.

Code analysis

The following is based on a decompiled version of Minecraft 1.12.1 using MCP 9.40pre-1 with new mappings. Code analysis by @unknown

The code line causing this exception to be thrown is (MCP line 43):

net.minecraft.client.gui.toasts.RecipeToast.draw()

toastGui.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI((EntityLivingBase)null, this.recipesOutputs.get((int)(delta / (5000L / (long)this.recipesOutputs.size()) % (long)this.recipesOutputs.size())), 8, 8);

The problem appears to be that the algorithm to determine which item of the item recipes the player receives should be shown in the recipe toast divides delta by zero whenever (long)this.recipesOutputs.size() is > 5000L.

net.minecraft.client.gui.toasts.RecipeToast.draw()

this.recipesOutputs.get((int)(delta / (5000L / (long)this.recipesOutputs.size()) % (long)this.recipesOutputs.size()))

Forge team fixed this issue by simply rearranging arithmetic operations in order to make divisions by zero impossible (given that this.recipesOutputs.size() can never be zero):

FIX: net.minecraft.client.gui.toasts.RecipeToast.draw()

toastGui.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI((EntityLivingBase)null, this.recipesOutputs.get((int)(delta * (long)this.recipesOutputs.size() / 5000L % (long)this.recipesOutputs.size())), 8, 8);

Linked issues

Attachments

Comments 4

Can confirm, looks like a resurgence of MC-115940.

Note: the game crashes (divides by zero) after 10 loops of the following set of 3 commands (hereafter referred to as function 'test:b') are invoked within 5 IRL seconds: 

/recipe give @p *
/recipe take @p *
/xp add @p 1 levels (optional, to show iteration count in xp bar)

The log says (1.13.2 vanilla):

11:13:07|avl|Loaded 524 recipes
[...]
11:14:02|net.minecraft.server.MinecraftServer|[player: Executed 3 commands from function 'test:b']
11:14:02|cgv|[CHAT] Executed 3 commands from function 'test:b'
[repeat previous 2 lines 8 more times]
11:14:08|cft|Unreported exception thrown!
java.lang.ArithmeticException: / by zero
    at chy.a(SourceFile:44)
    at cib$a.a(SourceFile:113)
    at cib.a(SourceFile:33)
    at cft.c(SourceFile:852)
    at cft.a(SourceFile:395) 
    at net.minecraft.client.main.Main.main(SourceFile:144)

Note that there are a total of 10 * 524 = 5,240 > 5,000 recipes (which is the value of (long)this.recipesOutputs.size()) that are being drawn in the "New recipe unlocked" toast, causing the division by 0 error to occur.

Can reproduce in 20w07a

Linus Nadler

Felix Jones

Confirmed

Commands, Crash

/recipe, command_block, crash

Minecraft 1.12.1, Minecraft 1.12.2, Minecraft 18w07c, Minecraft 1.13-pre5, Minecraft 1.13, ..., Minecraft 1.14 Pre-Release 4, Minecraft 1.14 Pre-Release 5, 1.15.2, 20w06a, 20w07a

20w17a

Retrieved