瀏覽代碼

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 <art27@cantab.net>

* add TEST

Co-authored-by: 6543 <6543@obermui.de>
tags/v1.16.0-rc1
zeripath 2 年之前
父節點
當前提交
206ea10b45
沒有連結到貢獻者的電子郵件帳戶。
共有 3 個檔案被更改,包括 7 行新增2 行删除
  1. 5
    0
      integrations/compare_test.go
  2. 1
    1
      routers/web/repo/compare.go
  3. 1
    1
      routers/web/repo/pull.go

+ 5
- 0
integrations/compare_test.go 查看文件



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


"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown") selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
// A dropdown for both base and head. // A dropdown for both base and head.
assert.Lenf(t, selection.Nodes, 2, "The template has changed") 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")
} }

+ 1
- 1
routers/web/repo/compare.go 查看文件

func CompareDiff(ctx *context.Context) { func CompareDiff(ctx *context.Context) {
ci := ParseCompareInfo(ctx) ci := ParseCompareInfo(ctx)
defer func() { defer func() {
if ci.HeadGitRepo != nil {
if ci != nil && ci.HeadGitRepo != nil {
ci.HeadGitRepo.Close() ci.HeadGitRepo.Close()
} }
}() }()

+ 1
- 1
routers/web/repo/pull.go 查看文件



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

Loading…
取消
儲存