From a2d95f62c4920ff0b238634029feb337baa6f7a7 Mon Sep 17 00:00:00 2001 From: ayuan <52194615+dan3577@users.noreply.github.com> Date: Thu, 3 Jul 2025 11:33:35 +0800 Subject: [PATCH] Update github.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在该函数中,有一行注释显示了一个严重错误:github.go:203-204GitHubBind 错误在第 204 行,代码从会话中检索用户 ID,但有一个带注释的第 203 行显示了原始(有问题的)实现:github.go:203// id := c.GetInt("id") // critical bug! 问题 原始的 bug 代码会尝试从 Gin 上下文中获取用户 ID,但这将失败,因为:c.GetInt("id") 用户 ID 不会在此端点的 Gin 上下文中自动设置 这可能会返回 0 或在尝试绑定 GitHub 帐户时导致 panic 然后,该函数将尝试更新 ID 为 0 的用户,而该 ID 不存在 --- controller/github.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/controller/github.go b/controller/github.go index 5e8df44..116fbeb 100644 --- a/controller/github.go +++ b/controller/github.go @@ -198,10 +198,26 @@ func GitHubBind(c *gin.Context) { }) return } - session := sessions.Default(c) - id := session.Get("id") - // id := c.GetInt("id") // critical bug! - user.Id = id.(int) +func GitHubBind(c *gin.Context) { + session := sessions.Default(c) + idInterface := session.Get("id") + if idInterface == nil { + c.JSON(http.StatusBadRequest, gin.H{ + "success": false, + "message": "用户未登录", + }) + return + } + + id, ok := idInterface.(int) + if !ok { + c.JSON(http.StatusInternalServerError, gin.H{ + "success": false, + "message": "用户ID类型错误", + }) + return + } + user.Id = id err = user.FillUserById() if err != nil { c.JSON(http.StatusOK, gin.H{