Skip to content

Commit ae51a90

Browse files
hiifongearl-warren
authored andcommitted
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) (cherry picked from commit b7614e2)
1 parent 06dfcc1 commit ae51a90

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
@@ -1117,7 +1117,10 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
11171117
} else {
11181118
actualBeforeCommitID := opts.BeforeCommitID
11191119
if len(actualBeforeCommitID) == 0 {
1120-
parentCommit, _ := commit.Parent(0)
1120+
parentCommit, err := commit.Parent(0)
1121+
if err != nil {
1122+
return nil, err
1123+
}
11211124
actualBeforeCommitID = parentCommit.ID.String()
11221125
}
11231126

@@ -1126,7 +1129,6 @@ func GetDiff(ctx context.Context, gitRepo *git.Repository, opts *DiffOptions, fi
11261129
AddDynamicArguments(actualBeforeCommitID, opts.AfterCommitID)
11271130
opts.BeforeCommitID = actualBeforeCommitID
11281131

1129-
var err error
11301132
beforeCommit, err = gitRepo.GetCommit(opts.BeforeCommitID)
11311133
if err != nil {
11321134
return nil, err

0 commit comments

Comments
 (0)