Parcourir la source

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 il y a 2 ans
Parent
révision
206ea10b45
Aucun compte lié à l'adresse e-mail de l'auteur
3 fichiers modifiés avec 7 ajouts et 2 suppressions
  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 Voir le fichier

@@ -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")
}

+ 1
- 1
routers/web/repo/compare.go Voir le fichier

@@ -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()
}
}()

+ 1
- 1
routers/web/repo/pull.go Voir le fichier

@@ -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()
}
}()

Chargement…
Annuler
Enregistrer