From d2f547a22a817c76747724debcc119b0de9d3c48 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Thu, 30 Sep 2021 17:31:15 +0100 Subject: [PATCH 1/2] 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 --- routers/web/repo/compare.go | 2 +- routers/web/repo/pull.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 71ca4df93e34..556a6218a6b8 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -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() } }() diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 1b15ea9a7951..9c9cf0f21dae 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -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() } }() From 2fc02b25ecb9f83a0fbd231e0a8f070ddb7b6a4d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 30 Sep 2021 18:53:27 +0200 Subject: [PATCH 2/2] add TEST --- integrations/compare_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/integrations/compare_test.go b/integrations/compare_test.go index 08468cbfd6e9..e8efe7cd0942 100644 --- a/integrations/compare_test.go +++ b/integrations/compare_test.go @@ -6,6 +6,7 @@ package integrations import ( "net/http" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -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") }