Skip to content

Commit b661bba

Browse files
author
Gusted
authored
backport(1.15): make ParsePatch more robust (#17580)
- Backport of #17573
1 parent 20ae184 commit b661bba

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

services/gitdiff/gitdiff.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,12 @@ parsingLoop:
831831
case strings.HasPrefix(line, "--- "):
832832
// Handle ambiguous filenames
833833
if curFile.IsAmbiguous {
834-
if len(line) > 6 && line[4] == 'a' {
834+
// The shortest string that can end up here is:
835+
// "--- a\t\n" without the qoutes.
836+
// This line has a len() of 7 but doesn't contain a oldName.
837+
// So the amount that the line need is at least 8 or more.
838+
// The code will otherwise panic for a out-of-bounds.
839+
if len(line) > 7 && line[4] == 'a' {
835840
curFile.OldName = line[6 : len(line)-1]
836841
if line[len(line)-2] == '\t' {
837842
curFile.OldName = curFile.OldName[:len(curFile.OldName)-1]
@@ -1186,6 +1191,10 @@ func readFileName(rd *strings.Reader) (string, bool) {
11861191
_ = rd.UnreadByte()
11871192
if char == '"' {
11881193
fmt.Fscanf(rd, "%q ", &name)
1194+
if len(name) == 0 {
1195+
log.Error("Reader has no file name: %v", rd)
1196+
return "", true
1197+
}
11891198
if name[0] == '\\' {
11901199
name = name[1:]
11911200
}

services/gitdiff/gitdiff_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,22 @@ func TestDiffToHTML_14231(t *testing.T) {
539539

540540
assertEqual(t, expected, output)
541541
}
542+
543+
func TestNoCrashes(t *testing.T) {
544+
type testcase struct {
545+
gitdiff string
546+
}
547+
548+
tests := []testcase{
549+
{
550+
gitdiff: "diff --git \n--- a\t\n",
551+
},
552+
{
553+
gitdiff: "diff --git \"0\n",
554+
},
555+
}
556+
for _, testcase := range tests {
557+
// It shouldn't crash, so don't care about the output.
558+
ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff))
559+
}
560+
}

0 commit comments

Comments
 (0)