Browse Source

Merge pull request #44 from digineo/fix_time_parsing

Do not crash on invalid lines
Julien Laffaye 10 years ago
parent
commit
8b4b15b841
3 changed files with 14 additions and 4 deletions
  1. 2 0
      .travis.yml
  2. 10 4
      ftp.go
  3. 2 0
      parse_test.go

+ 2 - 0
.travis.yml

@@ -3,9 +3,11 @@ go:
 - 1.5.1
 before_install:
 - sudo mkdir --mode 0777 -p /var/ftp/incoming
+- sudo apt-get update -qq
 - sudo apt-get install -qq vsftpd
 - sudo cp $TRAVIS_BUILD_DIR/.vsftpd.conf /etc/vsftpd.conf
 - sudo service vsftpd restart
+- sudo sysctl net.ipv6.conf.lo.disable_ipv6=0
 - go get github.com/axw/gocov/gocov
 - go get github.com/mattn/goveralls
 script:

+ 10 - 4
ftp.go

@@ -347,6 +347,10 @@ func parseLsListLine(line string) (*Entry, error) {
 		return e, nil
 	}
 
+	if len(fields) < 8 {
+		return nil, errUnsupportedListLine
+	}
+
 	if fields[1] == "0" {
 		e := &Entry{
 			Type: EntryTypeFile,
@@ -403,10 +407,12 @@ func parseDirListLine(line string) (*Entry, error) {
 
 	// Try various time formats that DIR might use, and stop when one works.
 	for _, format := range dirTimeFormats {
-		e.Time, err = time.Parse(format, line[:len(format)])
-		if err == nil {
-			line = line[len(format):]
-			break
+		if len(line) > len(format) {
+			e.Time, err = time.Parse(format, line[:len(format)])
+			if err == nil {
+				line = line[len(format):]
+				break
+			}
 		}
 	}
 	if err != nil {

+ 2 - 0
parse_test.go

@@ -58,6 +58,8 @@ var listTestsFail = []unsupportedLine{
 	{"drwxr-xr-x    3 110      1002            3 Dec 02  209 pub", "Invalid year format in time string"},
 	{"modify=20150806235817;invalid;UNIX.owner=0; movies", "Unsupported LIST line"},
 	{"Zrwxrwxrwx   1 root     other          7 Jan 25 00:17 bin -> usr/bin", "Unknown entry type"},
+	{"total 1", "Unsupported LIST line"},
+	{"", "Unsupported LIST line"},
 }
 
 func TestParseValidListLine(t *testing.T) {