From a5ddc2fcf7812c952e9a0baeb316df47c447aad8 Mon Sep 17 00:00:00 2001 From: Billy Keyes Date: Thu, 9 Sep 2021 14:20:33 -0400 Subject: [PATCH 1/2] Fix out-of-bounds panic parsing timestamps Found by go-fuzz. --- gitdiff/file_header.go | 2 +- gitdiff/file_header_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/gitdiff/file_header.go b/gitdiff/file_header.go index e3185bd..58904b4 100644 --- a/gitdiff/file_header.go +++ b/gitdiff/file_header.go @@ -527,7 +527,7 @@ func hasEpochTimestamp(s string) bool { // a valid timestamp can have optional ':' in zone specifier // remove that if it exists so we have a single format - if ts[len(ts)-3] == ':' { + if len(ts) >= 3 && ts[len(ts)-3] == ':' { ts = ts[:len(ts)-3] + ts[len(ts)-2:] } diff --git a/gitdiff/file_header_test.go b/gitdiff/file_header_test.go index 99cfed3..9f30eb7 100644 --- a/gitdiff/file_header_test.go +++ b/gitdiff/file_header_test.go @@ -724,6 +724,14 @@ func TestHasEpochTimestamp(t *testing.T) { Input: "+++ file.txt\t2019-03-21 12:34:56.789 -0700\n", Output: false, }, + "notTimstamp": { + Input: "+++ file.txt\trandom text\n", + Output: false, + }, + "notTimstampShort": { + Input: "+++ file.txt\t0\n", + Output: false, + }, } for name, test := range tests { From eea33b402dc1a991816fc447d86915f1af8b2f50 Mon Sep 17 00:00:00 2001 From: Billy Keyes Date: Thu, 9 Sep 2021 14:23:06 -0400 Subject: [PATCH 2/2] Fix test names --- gitdiff/file_header_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitdiff/file_header_test.go b/gitdiff/file_header_test.go index 9f30eb7..102795c 100644 --- a/gitdiff/file_header_test.go +++ b/gitdiff/file_header_test.go @@ -724,11 +724,11 @@ func TestHasEpochTimestamp(t *testing.T) { Input: "+++ file.txt\t2019-03-21 12:34:56.789 -0700\n", Output: false, }, - "notTimstamp": { + "notTimestamp": { Input: "+++ file.txt\trandom text\n", Output: false, }, - "notTimstampShort": { + "notTimestampShort": { Input: "+++ file.txt\t0\n", Output: false, },