From 9b627fa4d7213d0aa3a4fa0caca6ebd6680ec4de Mon Sep 17 00:00:00 2001 From: Test User Date: Sun, 10 May 2026 18:13:30 +0800 Subject: [PATCH] fix(billing): prevent nil pointer dereference in GetUsage 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 --- controller/billing.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/controller/billing.go b/controller/billing.go index e837157f03..c69f0a5e9d 100644 --- a/controller/billing.go +++ b/controller/billing.go @@ -69,7 +69,9 @@ func GetUsage(c *gin.Context) { if config.DisplayTokenStatEnabled { tokenId := c.GetInt(ctxkey.TokenId) token, err = model.GetTokenById(tokenId) - quota = token.UsedQuota + if err == nil && token != nil { + quota = token.UsedQuota + } } else { userId := c.GetInt(ctxkey.Id) quota, err = model.GetUserUsedQuota(userId)