txtar/archive: ignore invalid file separators

In case the file separator line does not define a filename, it should be
ignored.

Updates golang/go#47193

Change-Id: I3a0fee584c0f6b2b41814e79d20884d6468a3b76
Reviewed-on: https://go-review.googlesource.com/c/tools/+/336932
Run-TryBot: Jay Conrod <jayconrod@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Trust: Jay Conrod <jayconrod@google.com>
Trust: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Paschalis Tsilias 2021-07-24 01:19:56 +03:00 коммит произвёл Jay Conrod
Родитель 4fe0d6c80e
Коммит ebce39e5e3
2 изменённых файлов: 6 добавлений и 2 удалений

Просмотреть файл

@ -121,7 +121,7 @@ func isMarker(data []byte) (name string, after []byte) {
if i := bytes.IndexByte(data, '\n'); i >= 0 {
data, after = data[:i], data[i+1:]
}
if !bytes.HasSuffix(data, markerEnd) {
if !(bytes.HasSuffix(data, markerEnd) && len(data) >= len(marker)+len(markerEnd)) {
return "", nil
}
return strings.TrimSpace(string(data[len(marker) : len(data)-len(markerEnd)])), after

Просмотреть файл

@ -29,7 +29,10 @@ More file 1 text.
File 2 text.
-- empty --
-- noNL --
hello world`,
hello world
-- empty filename line --
some content
-- --`,
parsed: &Archive{
Comment: []byte("comment1\ncomment2\n"),
Files: []File{
@ -37,6 +40,7 @@ hello world`,
{"file 2", []byte("File 2 text.\n")},
{"empty", []byte{}},
{"noNL", []byte("hello world\n")},
{"empty filename line", []byte("some content\n-- --\n")},
},
},
},