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>
diff --git a/txtar/archive.go b/txtar/archive.go
index c384f33..2142566 100644
--- a/txtar/archive.go
+++ b/txtar/archive.go
@@ -121,7 +121,7 @@
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
diff --git a/txtar/archive_test.go b/txtar/archive_test.go
index 7ac5ee9..6534f53 100644
--- a/txtar/archive_test.go
+++ b/txtar/archive_test.go
@@ -29,7 +29,10 @@
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 @@
{"file 2", []byte("File 2 text.\n")},
{"empty", []byte{}},
{"noNL", []byte("hello world\n")},
+ {"empty filename line", []byte("some content\n-- --\n")},
},
},
},