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 int overflow when using fillr in negative y-coordinates #2082

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,17 @@ public int fillXZ(BlockVector3 origin, Pattern pattern, double radius, int depth
checkArgument(radius >= 0, "radius >= 0");
checkArgument(depth >= 1, "depth >= 1");

// Avoid int overflow (negative coordinate space allows for overflow back round to positive if the depth is large enough).
// Depth is always 1 or greater, thus the lower bound should always be <= origin y.
int lowerBound = origin.getBlockY() - depth + 1;
if (lowerBound > origin.getBlockY()) {
lowerBound = Integer.MIN_VALUE;
}

MaskIntersection mask = new MaskIntersection(
new RegionMask(new EllipsoidRegion(null, origin, Vector3.at(radius, radius, radius))),
new BoundedHeightMask(
Math.max(origin.getBlockY() - depth + 1, getWorld().getMinY()),
Math.max(lowerBound, getWorld().getMinY()),
Math.min(getWorld().getMaxY(), origin.getBlockY())),
Masks.negate(new ExistingBlockMask(this)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public int fill(Actor actor, LocalSession session, EditSession editSession,
radius = Math.max(1, radius);
we.checkMaxRadius(radius);
depth = Math.max(1, depth);
we.checkMaxRadius(depth);
Copy link
Member

Choose a reason for hiding this comment

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

So from looking at this, it doesn't actually appear that any of the changes in UtilityCommands.java are actually required here. And these do modify the behaviour of the commands.

Can this PR please be modified to only include the changes within EditSession.java?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, will do

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All done!


BlockVector3 pos = session.getPlacementPosition(actor);
int affected = editSession.fillXZ(pos, pattern, radius, depth, false);
Expand All @@ -123,8 +124,8 @@ public int fillr(Actor actor, LocalSession session, EditSession editSession,
Integer depth) throws WorldEditException {
radius = Math.max(1, radius);
we.checkMaxRadius(radius);
depth = depth == null ? Integer.MAX_VALUE : Math.max(1, depth);
we.checkMaxRadius(radius);
depth = depth == null ? (int) Math.round(radius) : Math.max(1, depth);
we.checkMaxRadius(depth);

BlockVector3 pos = session.getPlacementPosition(actor);
int affected = editSession.fillXZ(pos, pattern, radius, depth, true);
Expand Down