From 43e76cc7d0d92f983cc2dc3ba81d3922e39d55d1 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Tue, 1 Oct 2013 09:06:01 +0000 Subject: [PATCH] Fix UnixReader panic on empty lines. Added a test for the failure. Most of the scanner code looks like it's a modified version of bufio.ScanLines, so I changed it to use that but always add a line feed. --- provisioner/shell/unix_reader.go | 34 ++------------------------- provisioner/shell/unix_reader_test.go | 4 ++-- 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/provisioner/shell/unix_reader.go b/provisioner/shell/unix_reader.go index 5745dd291..0a19c0692 100644 --- a/provisioner/shell/unix_reader.go +++ b/provisioner/shell/unix_reader.go @@ -2,7 +2,6 @@ package shell import ( "bufio" - "bytes" "io" "sync" ) @@ -54,35 +53,6 @@ func (r *UnixReader) Read(p []byte) (n int, err error) { // only returns unix-style lines. So even if the line is "one\r\n", the // token returned will be "one\n". func scanUnixLine(data []byte, atEOF bool) (advance int, token []byte, err error) { - if atEOF && len(data) == 0 { - return 0, nil, nil - } - - if i := bytes.IndexByte(data, '\n'); i >= 0 { - // We have a new-line terminated line. Return the line with the newline - return i + 1, dropCR(data[0 : i+1]), nil - } - - if atEOF { - // We have a final, non-terminated line - return len(data), dropCR(data), nil - } - - if data[len(data)-1] != '\r' { - // We have a normal line, just let it tokenize - return len(data), data, nil - } - - // We need more data - return 0, nil, nil -} - -func dropCR(data []byte) []byte { - if len(data) > 0 && data[len(data)-2] == '\r' { - // Trim off the last byte and replace it with a '\n' - data = data[0 : len(data)-1] - data[len(data)-1] = '\n' - } - - return data + advance, token, err = bufio.ScanLines(data, atEOF) + return advance, append(token, "\n"...), err } diff --git a/provisioner/shell/unix_reader_test.go b/provisioner/shell/unix_reader_test.go index 17e16ae39..b498489ec 100644 --- a/provisioner/shell/unix_reader_test.go +++ b/provisioner/shell/unix_reader_test.go @@ -33,8 +33,8 @@ func TestUnixReader(t *testing.T) { } func TestUnixReader_unixOnly(t *testing.T) { - input := "one\ntwo\nthree\n" - expected := "one\ntwo\nthree\n" + input := "one\n\ntwo\nthree\n" + expected := "one\n\ntwo\nthree\n" r := &UnixReader{ Reader: bytes.NewReader([]byte(input)),