Skip to content

Fix null pointer panic in listFiles for empty repos and git worktrees #202

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 1 commit into from
Jul 7, 2025

Conversation

fgksgf
Copy link
Member

@fgksgf fgksgf commented Jul 7, 2025

Summary

Fixes a null pointer panic that occurs when using SkyWalking Eyes in empty git repositories or git worktree environments. The panic happens in pkg/header/check.go when attempting to call head.Hash() on a nil HEAD reference.

Problem

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x104ac1e48]

goroutine 1 [running]:
github.com/go-git/go-git/v5/plumbing.(*Reference).Hash(...)
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/go-git/go-git/[email protected]/plumbing/reference.go:274
github.com/apache/skywalking-eyes/pkg/header.listFiles(0x14000322400)
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/apache/[email protected]/pkg/header/check.go:96 +0x4d8
github.com/apache/skywalking-eyes/pkg/header.Check(0x14000322400, 0x140000ebbe0)
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/apache/[email protected]/pkg/header/check.go:42 +0x24
github.com/apache/skywalking-eyes/commands.init.func4(0x14000322100?, {0x14000431200, 0x0, 0x2})
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/apache/[email protected]/commands/header_check.go:45 +0x128
github.com/spf13/cobra.(*Command).execute(0x1059d7680, {0x140004311c0, 0x2, 0x2})
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/spf13/[email protected]/command.go:916 +0x680
github.com/spf13/cobra.(*Command).ExecuteC(0x1059d7c40)
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/spf13/[email protected]/command.go:1044 +0x320
github.com/spf13/cobra.(*Command).Execute(...)
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/spf13/[email protected]/command.go:968
github.com/apache/skywalking-eyes/commands.Execute()
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/apache/[email protected]/commands/root.go:62 +0x134
main.main()
        /Users/huaxi/.gvm/pkgsets/go1.24.3/global/pkg/mod/github.com/apache/[email protected]/cmd/license-eye/main.go:28 +0x1c

The issue occurs in the following scenarios:

  • Empty git repositories with no initial commits
  • Git worktrees with detached HEAD or invalid branch references
  • Corrupted repositories with invalid HEAD pointers
  • New worktrees created from non-existent commits or branches

The original code assumed repo.Head() would always return a valid HEAD reference, but this is not true in edge cases, leading to a panic when calling head.Hash().

Solution

  • Add proper null check for HEAD reference before calling head.Hash()
  • Add comprehensive error handling for repo.Head() to gracefully handle edge cases
  • Skip git-based file discovery when repository state is problematic
  • Gracefully fallback to glob-based file discovery to ensure functionality is maintained
  • Add test coverage for both empty repositories and git worktree scenarios

@fgksgf fgksgf added the bug Something isn't working label Jul 7, 2025
@fgksgf fgksgf requested a review from Copilot July 7, 2025 03:17
Copilot

This comment was marked as outdated.

@fgksgf fgksgf force-pushed the fix-null-pointer-panic branch from cb458af to 2b1a039 Compare July 7, 2025 03:27
… invalid HEAD

Fixes a panic that occurs when calling repo.Head().Hash() in empty git repositories,
git worktrees, or repositories with invalid HEAD references. The issue happened in
pkg/header/check.go where head could be nil, causing a null pointer dereference
when calling head.Hash().

This commonly occurs in the following scenarios:
- Empty git repositories with no initial commits
- Git worktrees with detached HEAD or invalid branch references
- Corrupted repositories with invalid HEAD pointers
- New worktrees created from non-existent commits or branches

Changes:
- Add proper null check for head reference before calling head.Hash()
- Add error handling for repo.Head() to gracefully handle edge cases
- Skip git-based file discovery when repository has no commits or invalid HEAD
- Add test cases for empty repositories and worktree scenarios in check_test.go
- Ensure graceful fallback to glob-based file discovery in problematic git states

Resolves panic at pkg/header/check.go:99 when working with empty git repositories
or git worktree environments with invalid HEAD references.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@fgksgf fgksgf force-pushed the fix-null-pointer-panic branch from 2b1a039 to 6b7c669 Compare July 7, 2025 03:47
@fgksgf fgksgf requested review from kezhenxu94 and Copilot July 7, 2025 03:47
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR prevents a nil pointer panic in listFiles by adding HEAD nil checks and error handling, and it adds tests for empty repositories and detached HEAD worktrees.

  • Added nil‐check and error handling around repo.Head() and commit lookup in pkg/header/check.go
  • Ensured fallback to file discovery via worktree status when HEAD is invalid
  • Introduced unit tests for empty repos and detached HEAD scenarios in pkg/header/check_test.go

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/header/check.go Added head == nil check and error handling around repo.Head()/CommitObject to avoid panic
pkg/header/check_test.go Added tests covering empty repo and detached HEAD worktree cases; imported go-git packages
Comments suppressed due to low confidence (1)

pkg/header/check_test.go:20

  • The test file uses os and filepath but these packages are not imported. Add "os" and "path/filepath" to the import block so functions like os.MkdirTemp and filepath.Join resolve correctly.
import (

Comment on lines +147 to +149
if len(fileList) == 0 {
t.Error("Expected to find at least one file")
}
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Instead of a manual if check and t.Error, use require.NotEmpty(t, fileList) for consistency and clearer failure messages in your tests.

Suggested change
if len(fileList) == 0 {
t.Error("Expected to find at least one file")
}
require.NotEmpty(t, fileList, "Expected to find at least one file")

Copilot uses AI. Check for mistakes.

if file == nil {
return errors.New("file pointer is nil")
head, err := repo.Head()
if err != nil || head == nil {
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This nested if/else block could be simplified with an early continue or extracted helper to reduce indentation and improve readability.

Copilot uses AI. Check for mistakes.

Copy link
Member

@kezhenxu94 kezhenxu94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kezhenxu94 kezhenxu94 merged commit 049742d into main Jul 7, 2025
1 check passed
@kezhenxu94 kezhenxu94 deleted the fix-null-pointer-panic branch July 7, 2025 03:50
@fgksgf fgksgf added this to the 0.8.0 milestone Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants