Skip to content

Clamp crafting plan scroll position to the element count - #242

Closed
PeanutSplash wants to merge 1 commit into
CyclopsMC:master-26-ltsfrom
PeanutSplash:fix/crafting-plan-sublist-out-of-bounds
Closed

PeanutSplash wants to merge 1 commit into
CyclopsMC:master-26-ltsfrom
PeanutSplash:fix/crafting-plan-sublist-out-of-bounds

Conversation

@PeanutSplash

Copy link
Copy Markdown

Bug

Opening the Crafting Jobs terminal, scrolling down in the crafting plan and letting the plan refresh can crash the client:

java.lang.IllegalArgumentException: fromIndex(74) > toIndex(35)
    at java.util.AbstractList.subListRangeCheck(AbstractList.java:508)
    at java.util.AbstractList.subList(AbstractList.java:484)
    at org.cyclops.integratedterminals.client.gui.container.component.GuiCraftingPlan.getVisibleElements(GuiCraftingPlan.java:146)
    at org.cyclops.integratedterminals.client.gui.container.ContainerScreenTerminalCraftingJobs.renderBg(...)

Happened on 1.20.4-1.4.13 in an FTB NeoTech instance (client crash, render thread).

Cause

getVisibleElements() only clamps the end index:

return this.visibleElements.subList(firstRow, Math.min(this.visibleElements.size(), firstRow + scrollBar.getVisibleRows()));

firstRow itself is never clamped, and setFirstRow() only guarantees firstRow >= 0.
When the plan is refreshed through inheritVisualizationState() -> refreshList() and the element list became shorter than the row the user had scrolled to (a crafting job progressing naturally removes steps), firstRow ends up past the end of the list and subList throws.

Two other places slice a scrollable list the same way and can hit the same exception:

  • ContainerScreenTerminalCraftingJobs#getVisiblePlans()
  • ContainerScreenTerminalStorageCraftingOptionAmount#drawOutputSlots()

Fix

  • Clamp the start index to the current size before slicing, so firstRow can never exceed the list length.
  • Additionally clamp the stored firstRow in GuiCraftingPlan#refreshList() so the scroll state stays consistent after the list shrinks (prevents the view from jumping back to the wrong row on the next scroll).

No behaviour change for valid scroll positions.

Notes

  • The same code (and the same flaw) exists on the other maintained branches (master-1.21*, master-1.20), so this may be worth backporting - let me know if you want me to open those PRs instead/too.
  • I verified the fix end-to-end in the 1.20.4 pack by patching the compiled classes the same way (clamping the start index before List.subList); the crash no longer reproduces when scrolling a plan that shrinks.
  • The three files pass a Java parser syntax check; I did not run ./gradlew build here, as the change is limited to index clamping.

getVisibleElements() passed firstRow as the start index of subList without
clamping it to the (possibly shrunken) element count. When a plan is
refreshed with fewer elements than the row the user had scrolled to -
which happens as a crafting job progresses - subList threw
IllegalArgumentException: fromIndex(74) > toIndex(35) and crashed the
client from the render thread.

Clamp the start index in the three places that slice a scrollable list
(GuiCraftingPlan, ContainerScreenTerminalCraftingJobs,
ContainerScreenTerminalStorageCraftingOptionAmount), and clamp the stored
first row when the element list is refreshed.
@CLAassistant

CLAassistant commented Nov 16, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rubensworks

Copy link
Copy Markdown
Member

Thanks for the PR, but this doesn't look like the right fix to me. The problem is that firstRow is improperly being set in the first place. I will make another fix.

@github-project-automation github-project-automation Bot moved this from To Do to Done in Maintenance Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants