diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2021-02-22 20:37:25 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2021-02-22 21:02:12 +0100 |
| commit | fcc6d71be2c3ce7d9305c04fc2e87af554571bac (patch) | |
| tree | b01dbb3d1e2988e28ea158d2d543d603ec0b9569 /vendor/github.com/sourcegraph/go-diff | |
| parent | 8f23c528ad5a943b9ffec5dcaf332fd0f614006e (diff) | |
go.mod: update golangci-lint to v1.37
Diffstat (limited to 'vendor/github.com/sourcegraph/go-diff')
| -rw-r--r-- | vendor/github.com/sourcegraph/go-diff/diff/parse.go | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/vendor/github.com/sourcegraph/go-diff/diff/parse.go b/vendor/github.com/sourcegraph/go-diff/diff/parse.go index 674350230..8d5cfc238 100644 --- a/vendor/github.com/sourcegraph/go-diff/diff/parse.go +++ b/vendor/github.com/sourcegraph/go-diff/diff/parse.go @@ -61,6 +61,7 @@ func (r *MultiFileDiffReader) ReadFile() (*FileDiff, error) { if e.Err == ErrNoFileHeader || e.Err == ErrExtendedHeadersEOF { return nil, io.EOF } + return nil, err case OverflowError: r.nextFileFirstLine = []byte(e) @@ -513,9 +514,22 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) { r.hunk.Section = section } else { // Read hunk body line. + + // If the line starts with `---` and the next one with `+++` we're + // looking at a non-extended file header and need to abort. + if bytes.HasPrefix(line, []byte("---")) { + ok, err := peekPrefix(r.reader, "+++") + if err != nil { + return r.hunk, err + } + if ok { + return r.hunk, &ParseError{r.line, r.offset, &ErrBadHunkLine{Line: line}} + } + } + + // If the line starts with the hunk prefix, this hunk is complete. if bytes.HasPrefix(line, hunkPrefix) { - // Saw start of new hunk, so this hunk is - // complete. But we've already read in the next hunk's + // But we've already read in the next hunk's // header, so we need to be sure that the next call to // ReadHunk starts with that header. r.nextHunkHeaderLine = line @@ -527,7 +541,7 @@ func (r *HunksReader) ReadHunk() (*Hunk, error) { return r.hunk, nil } - if len(line) >= 1 && (!linePrefix(line[0]) || bytes.HasPrefix(line, []byte("--- "))) { + if len(line) >= 1 && !linePrefix(line[0]) { // Bad hunk header line. If we're reading a multi-file // diff, this may be the end of the current // file. Return a "rich" error that lets our caller @@ -579,6 +593,19 @@ func linePrefix(c byte) bool { return false } +// peekPrefix peeks into the given reader to check whether the next +// bytes match the given prefix. +func peekPrefix(reader *bufio.Reader, prefix string) (bool, error) { + next, err := reader.Peek(len(prefix)) + if err != nil { + if err == io.EOF { + return false, nil + } + return false, err + } + return bytes.HasPrefix(next, []byte(prefix)), nil +} + // normalizeHeader takes a header of the form: // "@@ -linestart[,chunksize] +linestart[,chunksize] @@ section" // and returns two strings, with the first in the form: |
