Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent panic if status line is empty in mdstat #441

Merged
merged 1 commit into from May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion mdstat.go
Expand Up @@ -166,8 +166,12 @@ func parseMDStat(mdStatData []byte) ([]MDStat, error) {
}

func evalStatusLine(deviceLine, statusLine string) (active, total, down, size int64, err error) {
statusFields := strings.Fields(statusLine)
if len(statusFields) < 1 {
return 0, 0, 0, 0, fmt.Errorf("unexpected statusLine %q", statusLine)
}

sizeStr := strings.Fields(statusLine)[0]
sizeStr := statusFields[0]
size, err = strconv.ParseInt(sizeStr, 10, 64)
if err != nil {
return 0, 0, 0, 0, fmt.Errorf("unexpected statusLine %q: %w", statusLine, err)
Expand Down
17 changes: 12 additions & 5 deletions mdstat_test.go
Expand Up @@ -56,16 +56,23 @@ func TestFS_MDStat(t *testing.T) {
}

func TestInvalidMdstat(t *testing.T) {
invalidMount := []byte(`
invalidMount := [][]byte{[]byte(`
Personalities : [invalid]
md3 : invalid
314159265 blocks 64k chunks

unused devices: <none>
`)
`),
[]byte(`
md12 : active raid0 sdc2[0] sdd2[1]

_, err := parseMDStat(invalidMount)
if err == nil {
t.Fatalf("parsing of invalid reference file did not find any errors")
3886394368 blocks super 1.2 512k chunks
`)}

for _, invalid := range invalidMount {
_, err := parseMDStat(invalid)
if err == nil {
t.Fatalf("parsing of invalid reference file did not find any errors")
}
}
}