Skip to content

Commit 206ea10

Browse files
zeripath6543
andauthored
Prevent NPE on invalid diff (#17197)
* 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 #17193 Signed-off-by: Andrew Thornton <[email protected]> * add TEST Co-authored-by: 6543 <[email protected]>
1 parent 3bbdce2 commit 206ea10

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

integrations/compare_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integrations
66

77
import (
88
"net/http"
9+
"strings"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
@@ -21,4 +22,8 @@ func TestCompareTag(t *testing.T) {
2122
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
2223
// A dropdown for both base and head.
2324
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
25+
26+
req = NewRequest(t, "GET", "/user2/repo1/compare/invalid")
27+
resp = session.MakeRequest(t, req, http.StatusNotFound)
28+
assert.False(t, strings.Contains(resp.Body.String(), "/assets/img/500.png"), "expect 404 page not 500")
2429
}

routers/web/repo/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool
635635
func CompareDiff(ctx *context.Context) {
636636
ci := ParseCompareInfo(ctx)
637637
defer func() {
638-
if ci.HeadGitRepo != nil {
638+
if ci != nil && ci.HeadGitRepo != nil {
639639
ci.HeadGitRepo.Close()
640640
}
641641
}()

routers/web/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ func CompareAndPullRequestPost(ctx *context.Context) {
10431043

10441044
ci := ParseCompareInfo(ctx)
10451045
defer func() {
1046-
if ci.HeadGitRepo != nil {
1046+
if ci != nil && ci.HeadGitRepo != nil {
10471047
ci.HeadGitRepo.Close()
10481048
}
10491049
}()

0 commit comments

Comments
 (0)