Skip to content

Fix four nil pointer dereferences that crash the server#460

Merged
Volte6 merged 1 commit intoGoMudEngine:masterfrom
sarahmaeve:upstream-fix/nil-pointer-deref
Apr 6, 2026
Merged

Fix four nil pointer dereferences that crash the server#460
Volte6 merged 1 commit intoGoMudEngine:masterfrom
sarahmaeve:upstream-fix/nil-pointer-deref

Conversation

@sarahmaeve
Copy link
Copy Markdown
Contributor

Summary

Fixes four nil pointer dereference bugs that can panic and crash the server during normal operation.

Changes

  1. MoveToRoom (rooms/roommanager.go): users.GetByUserId can return nil for stale/invalid userId. Added nil check before accessing user.Character.RoomId.
  2. LogOutUserByConnectionId (users/users.go): When connection map contains an entry but user map does not, u is nil but dereferenced unconditionally. Moved map delete calls inside the u != nil guard and added cleanup of orphaned connection entries.
  3. TryRoomCommand (scripting/room.go): rooms.LoadRoom can return nil for destroyed ephemeral rooms. room.FindExitByName was called before the existing if room != nil check. Added early return.
  4. SaveRoomTemplate (rooms/save_and_load.go): roomManager.rooms lookup returns nil for new rooms not yet in memory. Added nil guard before ranging over .Containers.

Tests

  • 7 new tests across 3 packages verify each nil path is handled gracefully
  • Tests use require.NotPanics from testify to assert no panic on nil paths
  • Adds small test helper functions in users package for cross-package test setup

Test plan

  • All 7 new tests pass
  • Full test suite passes (pre-existing flaky test in procedural/ is unrelated)
  • Tests were written first to demonstrate the panics, then fixes applied

Add nil checks before dereference in:
- MoveToRoom: user lookup may return nil for stale/invalid userId
- LogOutUserByConnectionId: user record may be nil when connection
  map is inconsistent with user map
- TryRoomCommand: room may be nil for destroyed ephemeral rooms
- SaveRoomTemplate: room may not be in memory cache for new rooms

Adds 7 tests across 3 packages to verify each nil path is handled
gracefully. Adds test helpers in users package for cross-package
test setup.

Fixes #6

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sarahmaeve sarahmaeve requested a review from Volte6 as a code owner April 5, 2026 03:15
Copy link
Copy Markdown
Member

@Volte6 Volte6 left a comment

Choose a reason for hiding this comment

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

Thanks for your contributions!

@Volte6 Volte6 merged commit 418375d into GoMudEngine:master Apr 6, 2026
arinbb pushed a commit to arinbb/GoMud that referenced this pull request Apr 17, 2026
)

## Summary
Fixes four nil pointer dereference bugs that can panic and crash the server during normal operation.

## Changes
1. **MoveToRoom** (rooms/roommanager.go): users.GetByUserId can return nil for stale/invalid userId. Added nil check before accessing user.Character.RoomId.
2. **LogOutUserByConnectionId** (users/users.go): When connection map contains an entry but user map does not, u is nil but dereferenced unconditionally. Moved map delete calls inside the u != nil guard and added cleanup of orphaned connection entries.
3. **TryRoomCommand** (scripting/room.go): rooms.LoadRoom can return nil for destroyed ephemeral rooms. room.FindExitByName was called before the existing if room != nil check. Added early return.
4. **SaveRoomTemplate** (rooms/save_and_load.go): roomManager.rooms lookup returns nil for new rooms not yet in memory. Added nil guard before ranging over .Containers.

## Tests
- 7 new tests across 3 packages verify each nil path is handled gracefully
- Tests use require.NotPanics from testify to assert no panic on nil paths
- Adds small test helper functions in users package for cross-package test setup

## Test plan
- [x] All 7 new tests pass
- [x] Full test suite passes (pre-existing flaky test in procedural/ is unrelated)
- [x] Tests were written first to demonstrate the panics, then fixes applied
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