Skip to content

Commit

Permalink
Fix nil pointer dereference error when open link with invalid pull in…
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiCandong authored Aug 7, 2023
1 parent 049e400 commit 24fbf4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
55 changes: 28 additions & 27 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,51 +299,51 @@ func ForkPost(ctx *context.Context) {
ctx.Redirect(ctxUser.HomeLink() + "/" + url.PathEscape(repo.Name))
}

func checkPullInfo(ctx *context.Context) *issues_model.Issue {
func getPullInfo(ctx *context.Context) (issue *issues_model.Issue, ok bool) {
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if issues_model.IsErrIssueNotExist(err) {
ctx.NotFound("GetIssueByIndex", err)
} else {
ctx.ServerError("GetIssueByIndex", err)
}
return nil
return nil, false
}
if err = issue.LoadPoster(ctx); err != nil {
ctx.ServerError("LoadPoster", err)
return nil
return nil, false
}
if err := issue.LoadRepo(ctx); err != nil {
ctx.ServerError("LoadRepo", err)
return nil
return nil, false
}
ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
ctx.Data["Issue"] = issue

if !issue.IsPull {
ctx.NotFound("ViewPullCommits", nil)
return nil
return nil, false
}

if err = issue.LoadPullRequest(ctx); err != nil {
ctx.ServerError("LoadPullRequest", err)
return nil
return nil, false
}

if err = issue.PullRequest.LoadHeadRepo(ctx); err != nil {
ctx.ServerError("LoadHeadRepo", err)
return nil
return nil, false
}

if ctx.IsSigned {
// Update issue-user.
if err = activities_model.SetIssueReadBy(ctx, issue.ID, ctx.Doer.ID); err != nil {
ctx.ServerError("ReadBy", err)
return nil
return nil, false
}
}

return issue
return issue, true
}

func setMergeTarget(ctx *context.Context, pull *issues_model.PullRequest) {
Expand All @@ -361,14 +361,15 @@ func setMergeTarget(ctx *context.Context, pull *issues_model.PullRequest) {

// GetPullDiffStats get Pull Requests diff stats
func GetPullDiffStats(ctx *context.Context) {
issue := checkPullInfo(ctx)
issue, ok := getPullInfo(ctx)
if !ok {
return
}
pull := issue.PullRequest

mergeBaseCommitID := GetMergedBaseCommitID(ctx, issue)

if ctx.Written() {
return
} else if mergeBaseCommitID == "" {
if mergeBaseCommitID == "" {
ctx.NotFound("PullFiles", nil)
return
}
Expand Down Expand Up @@ -702,8 +703,8 @@ type pullCommitList struct {

// GetPullCommits get all commits for given pull request
func GetPullCommits(ctx *context.Context) {
issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}
resp := &pullCommitList{}
Expand Down Expand Up @@ -735,8 +736,8 @@ func ViewPullCommits(ctx *context.Context) {
ctx.Data["PageIsPullList"] = true
ctx.Data["PageIsPullCommits"] = true

issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}
pull := issue.PullRequest
Expand Down Expand Up @@ -779,8 +780,8 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
ctx.Data["PageIsPullList"] = true
ctx.Data["PageIsPullFiles"] = true

issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}
pull := issue.PullRequest
Expand Down Expand Up @@ -1016,8 +1017,8 @@ func ViewPullFilesForAllCommitsOfPr(ctx *context.Context) {

// UpdatePullRequest merge PR's baseBranch into headBranch
func UpdatePullRequest(ctx *context.Context) {
issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}
if issue.IsClosed {
Expand Down Expand Up @@ -1101,8 +1102,8 @@ func UpdatePullRequest(ctx *context.Context) {
// MergePullRequest response for merging pull request
func MergePullRequest(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.MergePullRequestForm)
issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}

Expand Down Expand Up @@ -1308,8 +1309,8 @@ func MergePullRequest(ctx *context.Context) {

// CancelAutoMergePullRequest cancels a scheduled pr
func CancelAutoMergePullRequest(ctx *context.Context) {
issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}

Expand Down Expand Up @@ -1447,8 +1448,8 @@ func CompareAndPullRequestPost(ctx *context.Context) {

// CleanUpPullRequest responses for delete merged branch when PR has been merged
func CleanUpPullRequest(ctx *context.Context) {
issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}

Expand Down
4 changes: 2 additions & 2 deletions routers/web/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ type viewedFilesUpdate struct {

func UpdateViewedFiles(ctx *context.Context) {
// Find corresponding PR
issue := checkPullInfo(ctx)
if ctx.Written() {
issue, ok := getPullInfo(ctx)
if !ok {
return
}
pull := issue.PullRequest
Expand Down

0 comments on commit 24fbf4e

Please sign in to comment.