Skip to content

fix(storage): fix member user nil panic #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2025
Merged

Conversation

littleniannian
Copy link
Collaborator

@littleniannian littleniannian commented May 28, 2025

User description

issue: https://github.com/actiontech/sqle-ee/issues/2374#issuecomment-2915631300


Description

  • 增加成员非空判断防止 nil panic

  • 调整获取用户成员项目方式


Changes walkthrough 📝

Relevant files
Bug fix
convert.go
增加 nil 检查及简化成员项获取逻辑                                                                           

internal/dms/storage/convert.go

  • 在 convertModelUser 中加入对 member 的非空校验
  • 在 convertModelMember 中仅选取第一个成员项目
  • +3/-3     

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @actiontech-bot actiontech-bot requested a review from BugsGuru May 28, 2025 09:31
    Copy link

    github-actions bot commented May 28, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit ce77e81)

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    空指针风险

    在转换成员信息时,仅检查了 m.User 是否为 nil 以及 m.User.Members 长度是否大于0,但未检查 m.User.Members[0].Project 是否为 nil。如果 Project 可能为 nil,则仍有可能引起 panic。建议增加对 m.User.Members[0].Project 的非空判断以确保安全。

    if m.User != nil && len(m.User.Members) > 0 {
    	projects = append(projects, m.User.Members[0].Project.Name)
    }

    Copy link

    github-actions bot commented May 28, 2025

    PR Code Suggestions ✨

    Latest suggestions up to ce77e81
    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    完整迭代成员检查

    建议遍历全部的 m.User.Members 而非仅使用第一个元素,并在访问 Project.Name 前检查 member.Project 是否为
    nil。这样可以确保不会因未处理的 nil 值而导致 panic,同时也能收集所有相关的项目名称。

    internal/dms/storage/convert.go [486-487]

    -if m.User != nil && len(m.User.Members) > 0 {
    -		projects = append(projects, m.User.Members[0].Project.Name)
    +if m.User != nil {
    +	for _, member := range m.User.Members {
    +		if member != nil && member.Project != nil {
    +			projects = append(projects, member.Project.Name)
    +		}
    +	}
     }
    Suggestion importance[1-10]: 8

    __

    Why: This suggestion enhances code safety by iterating over all elements in m.User.Members and adding a nil-check for member.Project before accessing its Name, preventing potential runtime panics and ensuring all project names are collected. The proposed change is directly derived from the diff context and improves functionality.

    Medium

    Previous suggestions

    Suggestions up to commit 1aa55f2
    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    检查切片长度

    建议在访问 m.User.Members[0] 之前加入对切片长度的判断,以防发生数组越界的 panic。这是一个严重问题,可能会导致程序在遇到空切片时崩溃。

    internal/dms/storage/convert.go [488-490]

    -if m.User != nil && m.User.Members != nil {
    +if m.User != nil && len(m.User.Members) > 0 {
     	projects = append(projects, m.User.Members[0].Project.Name)
     }
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion addresses a critical issue by checking the slice length before accessing m.User.Members[0], preventing a potential runtime panic. The improvement is both accurate and high impact.

    Medium

    Copy link

    Persistent review updated to latest commit ce77e81

    @BugsGuru BugsGuru merged commit 8c78d15 into main May 28, 2025
    1 check passed
    @BugsGuru BugsGuru deleted the fix_update_member_error branch May 28, 2025 10:41
    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