Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bounds checking in WorldSlice #2173

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

embeddedt
Copy link
Contributor

Currently, calling WorldSlice#getBlockState or related methods with a block position that wasn't cloned will either return null or throw ArrayIndexOutOfBoundsException. This tends to cause crashes in mods, and is not very useful for debugging.

To solve this, we check that the given position is within bounds, and return a sane default value if not:

  • Blocks.AIR.getDefaultState() for block states
  • 0 for light levels
  • null for block entities and block entity render attachments

Copy link
Member

@jellysquid3 jellysquid3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the section index against the bounds of the array will probably not do what you want. Each coordinate has to be checked for the out-of-range condition individually. For example, coordinates [4, 0, 0] will produce the same index as coordinates [1, 0, 1]. While that is still in bounds of the flat array, it will produce hard to debug issues that manifest as seemingly random corruption.

@jellysquid3
Copy link
Member

jellysquid3 commented Nov 20, 2023

Some other thoughts:

  • We could just initialize the block data arrays to the default state, rather than leaving null entries in there. That would eliminate the need for checking if there is a null entry.
  • The implementation could be changed to store a single 34x34x34 array of block data, rather than a table of sections -> block data arrays. That way, we can consolidate the "out of bounds" and "no data exists" cases into one, and simplify the code a bit. This might actually be faster, anyways, since it reduces the amount of indirection and index calculations.

@jellysquid3 jellysquid3 merged commit 105c326 into CaffeineMC:dev Dec 1, 2023
@embeddedt embeddedt deleted the fix/worldslice-oob branch December 1, 2023 00:57
IMS212 pushed a commit to IMS212/sodium-fabric that referenced this pull request Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants