Skip to content

Commit b7614e2

Browse files
authored
Fix parentCommit invalid memory address or nil pointer dereference. (go-gitea#33204)
When the parent Commit does not exist on gitea, an error will be reported when opening the Commit details page: invalid memory address or nil pointer dereference. ![image](https://github.com/user-attachments/assets/4c2a9802-935f-41e9-b5b9-a4f0d745f709) ![image](https://github.com/user-attachments/assets/7b0bc15e-7f5f-4d58-8d24-fee667a799fa)
1 parent dc2308a commit b7614e2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

modules/git/diff.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
6464
} else if commit.ParentCount() == 0 {
6565
cmd.AddArguments("show").AddDynamicArguments(endCommit).AddDashesAndList(files...)
6666
} else {
67-
c, _ := commit.Parent(0)
67+
c, err := commit.Parent(0)
68+
if err != nil {
69+
return err
70+
}
6871
cmd.AddArguments("diff", "-M").AddDynamicArguments(c.ID.String(), endCommit).AddDashesAndList(files...)
6972
}
7073
case RawDiffPatch:
@@ -74,7 +77,10 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff
7477
} else if commit.ParentCount() == 0 {
7578
cmd.AddArguments("format-patch", "--no-signature", "--stdout", "--root").AddDynamicArguments(endCommit).AddDashesAndList(files...)
7679
} else {
77-
c, _ := commit.Parent(0)
80+
c, err := commit.Parent(0)
81+
if err != nil {
82+
return err
83+
}
7884
query := fmt.Sprintf("%s...%s", endCommit, c.ID.String())
7985
cmd.AddArguments("format-patch", "--no-signature", "--stdout").AddDynamicArguments(query).AddDashesAndList(files...)
8086
}

services/gitdiff/gitdiff.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,10 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
11361136
} else {
11371137
actualBeforeCommitID := opts.BeforeCommitID
11381138
if len(actualBeforeCommitID) == 0 {
1139-
parentCommit, _ := commit.Parent(0)
1139+
parentCommit, err := commit.Parent(0)
1140+
if err != nil {
1141+
return nil, err
1142+
}
11401143
actualBeforeCommitID = parentCommit.ID.String()
11411144
}
11421145

@@ -1145,7 +1148,6 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
11451148
AddDynamicArguments(actualBeforeCommitID, opts.AfterCommitID)
11461149
opts.BeforeCommitID = actualBeforeCommitID
11471150

1148-
var err error
11491151
beforeCommit, err = gitRepo.GetCommit(opts.BeforeCommitID)
11501152
if err != nil {
11511153
return nil, err

0 commit comments

Comments
 (0)