Skip to content

Commit efff7e0

Browse files
committed
fix: guard checkDiffFile against mode-only diffs with empty OrigName/NewName
Mode-only diffs (e.g. chmod changes) and some binary diffs produce no --- / +++ file headers in the unified diff output. The sourcegraph/go-diff parser therefore returns empty OrigName and NewName strings for those FileDiff entries. checkDiffFile assumed at least one '/' was always present (from the 'a/' or 'b/' prefix) and unconditionally accessed index [1] after SplitN, causing a runtime panic: panic: runtime error: index out of range [1] with length 1 diff/diff.go:44 Guard the slice length before indexing and skip entries with no file headers — there is no content to scan in mode-only or header-less diffs.
1 parent f707d77 commit efff7e0

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

diff/diff.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func checkDiffFile(parsedDiff *diff.FileDiff, workspace string) (filePath string
4141
// If file is being renamed we don't want to check it for flags.
4242
parsedFileA := strings.SplitN(parsedDiff.OrigName, "/", 2)
4343
parsedFileB := strings.SplitN(parsedDiff.NewName, "/", 2)
44+
// Mode-only diffs (e.g. chmod changes) and some binary diffs emit no --- / +++ headers.
45+
// go-diff returns empty OrigName/NewName for these entries; there is no content to scan.
46+
if len(parsedFileA) < 2 || len(parsedFileB) < 2 {
47+
return "", true
48+
}
4449
fullPathToA := workspace + "/" + parsedFileA[1]
4550
fullPathToB := workspace + "/" + parsedFileB[1]
4651
info, err := os.Stat(fullPathToB)
@@ -90,4 +95,4 @@ func ProcessDiffs(matcher lsearch.Matcher, contents []byte, builder *refs.Refere
9095
break
9196
}
9297
}
93-
}
98+
}

0 commit comments

Comments
 (0)