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

fix: don't process out of bound section while trimming Y sections #2902

Merged
merged 6 commits into from
Sep 11, 2024

Conversation

PierreSchwang
Copy link
Member

Overview

Fixes #2894

Description

trimY now doesn't overflow into unrelated sections anymore, allowing out of bounds editing. Seems to work for me so far - tests are passing. I couldn't find any edge cases or how that might break other existing behavior.

I'd highly appreciate additional tests by others.

Submitter Checklist

Preview Give feedback

@PierreSchwang PierreSchwang requested a review from a team as a code owner August 30, 2024 16:00
@github-actions github-actions bot added the Bugfix This PR fixes a bug label Aug 30, 2024
int minLayer = (minY - 1) >> 4;
int maxLayer = (maxY + 1) >> 4;
int minLayer = minY >> 4;
int maxLayer = maxY >> 4;
Copy link
Member Author

@PierreSchwang PierreSchwang Aug 30, 2024

Choose a reason for hiding this comment

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

It would theoretically work with maxY + 1 when reverting the change in L97 (the ternary) - but that does not really feel wanted.
Currently, when crossing the chunk section border on the upper end (e.g. Y 255), the maxLayer is the section from 256-271. That still is fine and works - the layer of the section representing maxY is set to __RESERVED__ as it should - but the chunk section from 256-271 is set to 4096 chars only containing __RESERVED__ instead of just being set to null.
With the change, that now is more consistent. In the previous example, the section is filled with the correct blocks from Y 240-254 and 255 is set to __RESERVED__. The section 256-271 is set to null.

Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense to clarify the meaning of minLayer and maxLayer. From my understanding, minLayer is the highest layer that should be partially trimmed below, and maxLayer is the lowest layer that should be partially trimmed above.

The edge cases here are, when not adding/subtracting before the shift, when minY % 16 == 0 (the full section should not be trimmed) or maxY % 16 == 15 (the full section should not be trimmed,).

In the minLayer case, that's easy because 0 << 8 == 0, so the loop won't run.
In the maxLayer case, 15 << 8 == 3840, means we would clear one layer too early. But (15 + 1) % 16 == 0, so adding the layer this way results in the whole section being wiped. But I think moving the addition should work: 15 % 16 == 15,(15 + 1) << 8 == 4096, so ((maxX & 15) + 1) << 8 should simplify the maxLayer case.

That's at least my understanding, maybe I missed something.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've simplified the logic itself a bit which should make the whole thing easier to understand I'd say? (At least it does for me). And yeah, the simplification should work (and does based on my testing).

If the new loop logic is alright, I could (and should) apply that to the other part of the trimY method (where keepInsideRange is false)

Copy link
Member

@SirYwell SirYwell left a comment

Choose a reason for hiding this comment

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

Looks good now.

@dordsor21 dordsor21 merged commit f771b0c into main Sep 11, 2024
11 checks passed
@dordsor21 dordsor21 deleted the fix/2894 branch September 11, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix This PR fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Region Restrictions incorrectly protect regions
3 participants