From 20eaca6d05ddeb56f5a76638e84211117c3f0131 Mon Sep 17 00:00:00 2001 From: pricly-yellow <79628427+pricly-yellow@users.noreply.github.com> Date: Tue, 5 Oct 2021 21:41:48 +0700 Subject: Fix stange behavior of DownloadPullDiffOrPatch in incorect index (#17223) Fix GetPullRequestByIndex by validate index > 1 Signed-off-by: Danila Kryukov Co-authored-by: a1012112796 <1012112796@qq.com> --- models/pull.go | 3 +++ models/pull_test.go | 4 ++++ 2 files changed, 7 insertions(+) (limited to 'models') diff --git a/models/pull.go b/models/pull.go index 004af62f03..38f2c1b8ce 100644 --- a/models/pull.go +++ b/models/pull.go @@ -522,6 +522,9 @@ func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest, // GetPullRequestByIndex returns a pull request by the given index func GetPullRequestByIndex(repoID, index int64) (*PullRequest, error) { + if index < 1 { + return nil, ErrPullRequestNotExist{} + } pr := &PullRequest{ BaseRepoID: repoID, Index: index, diff --git a/models/pull_test.go b/models/pull_test.go index 2b7ef2f664..173977aafe 100644 --- a/models/pull_test.go +++ b/models/pull_test.go @@ -134,6 +134,10 @@ func TestGetPullRequestByIndex(t *testing.T) { _, err = GetPullRequestByIndex(9223372036854775807, 9223372036854775807) assert.Error(t, err) assert.True(t, IsErrPullRequestNotExist(err)) + + _, err = GetPullRequestByIndex(1, 0) + assert.Error(t, err) + assert.True(t, IsErrPullRequestNotExist(err)) } func TestGetPullRequestByID(t *testing.T) { -- cgit v1.2.3