Skip to content

Bug: CanAccess in StarportLayoutDefinition lacks null guards, can throw NullReferenceException at runtime #1976

@coderabbitai

Description

@coderabbitai

Summary

In SWLOR.Game.Server/Feature/PropertyLayoutDefinition/StarportLayoutDefinition.cs, PR #1974 added null checks to the OnSpawn callback, but the CanAccess method (called every time a player interacts with a starport terminal) has no equivalent protection. If a player is inside a starport whose parent record has been deleted or orphaned, DB.Get returns null and the subsequent property chain access throws a NullReferenceException.

Affected Code

var dbProperty = DB.Get<WorldProperty>(propertyId);        // could be null
var dbBuilding = DB.Get<WorldProperty>(dbProperty.ParentPropertyId); // NullReferenceException if dbProperty is null
var cityId = dbBuilding.ParentPropertyId;                  // NullReferenceException if dbBuilding is null

Suggested Fix

Mirror the null guards added to OnSpawn:

if (dbProperty == null)
{
    SendMessageToPC(player, "This terminal is unavailable. Please notify an administrator.");
    return false;
}
var dbBuilding = DB.Get<WorldProperty>(dbProperty.ParentPropertyId);
if (dbBuilding == null)
{
    SendMessageToPC(player, "This terminal is unavailable. Please notify an administrator.");
    return false;
}

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions