Skip to content

Guard single-corner geometry smoothing against missing portal neighbors#1200

Open
Copilot wants to merge 9 commits into
developfrom
copilot/fix-null-reference-exception
Open

Guard single-corner geometry smoothing against missing portal neighbors#1200
Copilot wants to merge 9 commits into
developfrom
copilot/fix-null-reference-exception

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 19, 2026

Selecting a wall corner and applying geometry hotkeys could throw a NullReferenceException inside EditorActions.EditSectorGeometry. The failure came from the single-corner smoothing path assuming portal/boundary neighbor lookups always returned a valid RoomSectorPair.

  • Problem

    • Single-corner smoothing dereferenced originSector / cornerSectors without validating that the portal lookup resolved to a real room-sector pair.
    • This could fail on wall selections near boundaries or non-resolvable adjoining sectors.
  • Change

    • Added null-safe handling for the single-corner smoothing branch in EditSectorGeometry.
    • If the origin lookup is invalid, the corner comparison is skipped entirely.
    • If an individual neighboring corner lookup is invalid, that corner is treated as non-matching instead of crashing.
  • Implementation detail

    • Extracted the corner height comparison into a small local helper to make the validity check explicit and keep the smoothing branch readable.
bool IsCornerValidAndAtSameHeight(RoomSectorPair cornerSector, SectorEdge edge, SectorVerticalPart cornerVertical, int referenceHeight)
{
    if (cornerSector.Sector == null || cornerSector.Room == null)
        return false;

    return referenceHeight == cornerSector.Sector.GetHeight(cornerVertical, edge) + cornerSector.Room.Position.Y;
}

Copilot AI and others added 8 commits April 19, 2026 21:05
Agent-Logs-Url: https://github.com/TombEngine/Tomb-Editor/sessions/c0329567-576d-4e94-be74-c2b46e121f4c

Co-authored-by: Nickelony <20436882+Nickelony@users.noreply.github.com>
Agent-Logs-Url: https://github.com/TombEngine/Tomb-Editor/sessions/c0329567-576d-4e94-be74-c2b46e121f4c

Co-authored-by: Nickelony <20436882+Nickelony@users.noreply.github.com>
Agent-Logs-Url: https://github.com/TombEngine/Tomb-Editor/sessions/c0329567-576d-4e94-be74-c2b46e121f4c

Co-authored-by: Nickelony <20436882+Nickelony@users.noreply.github.com>
Agent-Logs-Url: https://github.com/TombEngine/Tomb-Editor/sessions/c0329567-576d-4e94-be74-c2b46e121f4c

Co-authored-by: Nickelony <20436882+Nickelony@users.noreply.github.com>
Agent-Logs-Url: https://github.com/TombEngine/Tomb-Editor/sessions/c0329567-576d-4e94-be74-c2b46e121f4c

Co-authored-by: Nickelony <20436882+Nickelony@users.noreply.github.com>
Agent-Logs-Url: https://github.com/TombEngine/Tomb-Editor/sessions/c0329567-576d-4e94-be74-c2b46e121f4c

Co-authored-by: Nickelony <20436882+Nickelony@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullReferenceException when editing sector geometry Guard single-corner geometry smoothing against missing portal neighbors Apr 19, 2026
Copilot AI requested a review from Nickelony April 19, 2026 21:11
@Nickelony Nickelony added bug Something isn't working as intended. ready for review The Pull Request is finished and ready for review. labels Apr 19, 2026
@Nickelony Nickelony marked this pull request as ready for review April 19, 2026 21:29
@Nickelony
Copy link
Copy Markdown
Collaborator

@copilot Would you be able to provide reproduction steps for the original issue? What do we have to do in order to get the original error before the fix was applied? Also, please double-check that the changes don't cause any regressions for other places in the code.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 20, 2026

@copilot Would you be able to provide reproduction steps for the original issue? What do we have to do in order to get the original error before the fix was applied? Also, please double-check ...

Most likely repro before f166289 was:

  1. Open the editor in Geometry mode.
  2. Select a single wall sector that sits on a room boundary or on a wall portal where one diagonal neighbor lookup can fall outside the room / into a missing adjoining sector.
  3. Click the same selection with Ctrl until the selection arrow becomes a single corner (CornerNW/NE/SE/SW).
  4. Trigger a smooth geometry hotkey on that selection: Alt+Q / Alt+A for floor, or Alt+W / Alt+S for ceiling.

That path goes through GenericDirectionalControlCommand() into EditSectorGeometry(..., smooth: true). Before the fix, the single-corner smoothing block dereferenced originSector and cornerSectors[i] without checking whether GetSectorTryThroughPortal() had returned a valid room/sector pair, which is what caused the null reference.

I also re-checked the regression scope on f166289: the change only affects the area.Width == -1 && area.Height == -1 && arrow > ArrowType.EdgeW smooth single-corner case. Non-smooth edits, edge smoothing, and multi-sector smoothing are unchanged. For valid neighbors the height comparison is the same as before; only invalid portal/boundary lookups now short-circuit, which matches the null-safe behavior already used elsewhere in the smoothing code.

@Nickelony Nickelony added this to the Version 2.0 milestone Apr 20, 2026
@Nickelony
Copy link
Copy Markdown
Collaborator

Managed to replicate it:
image

Simply CTRL+Click on room boundary wall like this:
image
Then when you see the X, press ALT+Q and it crashes.

This PR has actually fixed it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Guards the geometry smoothing logic in EditorActions.EditSectorGeometry so single-corner smoothing no longer crashes when portal/boundary neighbor sector lookups fail, improving editor stability during corner-based geometry hotkeys.

Changes:

  • Added null-safe handling when resolving the “origin” sector for single-corner smoothing.
  • Added null-safe handling for each corner-neighbor sector comparison via a small local helper.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as intended. ready for review The Pull Request is finished and ready for review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TE - System.NullReferenceException: Object reference not set to an instance of an object. at TombEditor.EditorActions.EditSectorGeometry...

3 participants