Skip to content

🔧 fix: ensure farmer exists before accessing tokens#1937

Merged
mkmccarty merged 1 commit intomainfrom
mm-branch-1
Dec 12, 2025
Merged

🔧 fix: ensure farmer exists before accessing tokens#1937
mkmccarty merged 1 commit intomainfrom
mm-branch-1

Conversation

@mkmccarty
Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings December 12, 2025 22:50
@mkmccarty mkmccarty merged commit e8c143b into main Dec 12, 2025
14 checks passed
@mkmccarty mkmccarty deleted the mm-branch-1 branch December 12, 2025 22:50
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

This PR adds a nil check to the GetTokens function to prevent potential nil pointer dereference errors when accessing a farmer's tokens. The fix ensures that if a farmer doesn't exist for the given userID, a new farmer is created before attempting to access their token count.

Key Changes

  • Added nil check and newFarmer() call in GetTokens function to ensure farmer exists before token access
  • Aligns with the existing defensive programming pattern used in other getter/setter functions in the file
Comments suppressed due to low confidence (1)

src/farmerstate/farmerstate.go:235

  • The nil check on lines 229-231 makes the subsequent check on line 232 redundant. After calling newFarmer(userID), farmerstate[userID] is guaranteed to be non-nil, so the ok check will always be true. Consider simplifying this to directly return farmerstate[userID].Tokens after the nil check, similar to the pattern used in GetMissionShipSecondary (lines 208-213).
	if farmerstate, ok := farmerstate[userID]; ok {
		return farmerstate.Tokens
	}
	return 0

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