Skip to content

fix(billing): prevent nil pointer dereference in GetUsage#2389

Open
hobostay wants to merge 1 commit into
songquanpeng:mainfrom
hobostay:fix/billing-nil-pointer
Open

fix(billing): prevent nil pointer dereference in GetUsage#2389
hobostay wants to merge 1 commit into
songquanpeng:mainfrom
hobostay:fix/billing-nil-pointer

Conversation

@hobostay

Copy link
Copy Markdown

Summary

  • Add nil and error checks before accessing token.UsedQuota in GetUsage().

Bug

In controller/billing.go, when DisplayTokenStatEnabled is true, the code calls model.GetTokenById(tokenId) and immediately accesses token.UsedQuota without checking if token is nil or if an error occurred.

GetTokenById returns nil when id == 0 (which can happen if the TokenId context key is missing), causing a nil pointer dereference panic.

Test plan

  • Verify the /api/billing/usage endpoint works correctly with a valid token
  • Verify no panic occurs when called without a valid token ID

🤖 Generated with Claude Code

When `DisplayTokenStatEnabled` is true and `GetTokenById` returns an
error (e.g. tokenId is 0), `token` can be nil. Accessing
`token.UsedQuota` without checking causes a panic.

Add nil and error checks before accessing token fields.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

1 participant