Skip to content

Commit

Permalink
Prevent NPE on invalid diff (go-gitea#17197)
Browse files Browse the repository at this point in the history
* Prevent NPE on invalid diff

If ParseCompareInfo returns a nil compare info the defer function needs to ensure
that it does not attempt to close the HeadGitRepo.

Fix go-gitea#17193

Signed-off-by: Andrew Thornton <[email protected]>

* add TEST

Co-authored-by: 6543 <[email protected]>
  • Loading branch information
zeripath and 6543 authored Sep 30, 2021
1 parent 3bbdce2 commit 206ea10
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions integrations/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package integrations

import (
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,4 +22,8 @@ func TestCompareTag(t *testing.T) {
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
// A dropdown for both base and head.
assert.Lenf(t, selection.Nodes, 2, "The template has changed")

req = NewRequest(t, "GET", "/user2/repo1/compare/invalid")
resp = session.MakeRequest(t, req, http.StatusNotFound)
assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500")
}
2 changes: 1 addition & 1 deletion routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool
func CompareDiff(ctx *context.Context) {
ci := ParseCompareInfo(ctx)
defer func() {
if ci.HeadGitRepo != nil {
if ci != nil && ci.HeadGitRepo != nil {
ci.HeadGitRepo.Close()
}
}()
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {

ci := ParseCompareInfo(ctx)
defer func() {
if ci.HeadGitRepo != nil {
if ci != nil && ci.HeadGitRepo != nil {
ci.HeadGitRepo.Close()
}
}()
Expand Down

0 comments on commit 206ea10

Please sign in to comment.