collector: add bounds check and test for filesystem collector (#1133)

Signed-off-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Matt Layher 2018-10-30 17:12:42 -04:00 committed by Ben Kochie
commit c0a55e3f80
2 changed files with 35 additions and 2 deletions

View file

@ -17,6 +17,8 @@ package collector
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"sync"
@ -139,11 +141,20 @@ func mountPointDetails() ([]filesystemLabels, error) {
}
defer file.Close()
filesystems := []filesystemLabels{}
scanner := bufio.NewScanner(file)
return parseFilesystemLabels(file)
}
func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {
var filesystems []filesystemLabels
scanner := bufio.NewScanner(r)
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
if len(parts) < 4 {
return nil, fmt.Errorf("malformed mount point information: %q", scanner.Text())
}
// Ensure we handle the translation of \040 and \011
// as per fstab(5).
parts[1] = strings.Replace(parts[1], "\\040", " ", -1)
@ -156,5 +167,6 @@ func mountPointDetails() ([]filesystemLabels, error) {
options: parts[3],
})
}
return filesystems, scanner.Err()
}