Skip to content

[Upstream] fix(billing): prevent nil pointer dereference in GetUsage#131

Open
Sagit-chu wants to merge 1 commit into
mainfrom
upstream-pr-2389
Open

[Upstream] fix(billing): prevent nil pointer dereference in GetUsage#131
Sagit-chu wants to merge 1 commit into
mainfrom
upstream-pr-2389

Conversation

@Sagit-chu

Copy link
Copy Markdown
Owner

Synced from upstream PR: songquanpeng#2389

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