diff options
author | Xing Xin <xingxin.xx@bytedance.com> | 2023-06-29 12:13:58 +0300 |
---|---|---|
committer | Xing Xin <xingxin.xx@bytedance.com> | 2023-06-30 11:02:41 +0300 |
commit | af8dac3091c3652636ab55957e2d1d676bc5a263 (patch) | |
tree | 488b08d042b82b390cc99816cf1aaa983706efad | |
parent | 48ce7b2f5c702bc6ba3e9b7c7a529cfad57710c5 (diff) |
merge-tree: Fix length checking for conflict parsing
The length checking for conflicting file parsing can not protect us from
a out-of-range panic if we received malformed output from git, though it
can hardly happen.
Signed-off-by: Xing Xin <xingxin.xx@bytedance.com>
-rw-r--r-- | internal/git/localrepo/merge.go | 2 | ||||
-rw-r--r-- | internal/git/localrepo/merge_test.go | 16 |
2 files changed, 17 insertions, 1 deletions
diff --git a/internal/git/localrepo/merge.go b/internal/git/localrepo/merge.go index 936a5758f..974224eb9 100644 --- a/internal/git/localrepo/merge.go +++ b/internal/git/localrepo/merge.go @@ -162,7 +162,7 @@ func parseMergeTreeError(objectHash git.ObjectHash, cfg mergeTreeConfig, output return "", structerr.NewInternal("converting stage to int: %w", err) } - if i+numOfPaths+2 > len(fields) { + if i+numOfPaths+2 >= len(fields) { return "", structerr.NewInternal("incorrect number of fields: %s", infoMsg) } diff --git a/internal/git/localrepo/merge_test.go b/internal/git/localrepo/merge_test.go index 47dec2eda..5383c17fb 100644 --- a/internal/git/localrepo/merge_test.go +++ b/internal/git/localrepo/merge_test.go @@ -853,6 +853,22 @@ func TestParseResult(t *testing.T) { }, }, }, + { + desc: "invalid number of fields in conflicting file section", + output: strings.Join([]string{ + gittest.DefaultObjectHash.EmptyTreeOID.String(), + fmt.Sprintf("100644 %s 1\ta", gittest.DefaultObjectHash.EmptyTreeOID), + fmt.Sprintf("100644 %s 2\ta", gittest.DefaultObjectHash.EmptyTreeOID), + fmt.Sprintf("100644 %s 3\ta", gittest.DefaultObjectHash.EmptyTreeOID), + "", + "1", + "a", + "Auto-merging a\n", + "", + }, "\x00"), + oid: gittest.DefaultObjectHash.EmptyTreeOID, + expectedErr: structerr.NewInternal("incorrect number of fields: %s", "1\x00a\x00Auto-merging a\n\x00"), + }, } for _, tc := range testCases { |