Skip to content

Commit

Permalink
Prevent panic if status line is empty in mdstat
Browse files Browse the repository at this point in the history
This change fixes a potential source of panic when the status line in
mdstat is empty and the code is trying to reference first item in a
zero-sized slice.

Signed-off-by: Artur Makutunowicz <arma@linkedin.com>
  • Loading branch information
amakutunowicz committed Apr 14, 2022
1 parent d917e64 commit 3f75f0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
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")
}
}
}

0 comments on commit 3f75f0d

Please sign in to comment.