Skip to content

Commit 4c06e33

Browse files
phyberdiscordianfish
authored andcommitted
filesystem_freebsd: Fix label values
We must know the length of the various filesystem C strings before turning them from a byte array into a Go string, otherwise our Go strings could contain null bytes, corrupting the label values. Signed-off-by: David O'Rourke <david.orourke@gmail.com>
1 parent 2c433cd commit 4c06e33

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

collector/filesystem_freebsd.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package collector
1717

1818
import (
19+
"bytes"
20+
1921
"github.com/go-kit/kit/log/level"
2022
"golang.org/x/sys/unix"
2123
)
@@ -40,14 +42,19 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
4042
}
4143
stats := []filesystemStats{}
4244
for _, fs := range buf {
43-
mountpoint := string(fs.Mntonname[:])
45+
// We need to work out the lengths of the actual strings here,
46+
// otherwuse we will end up with null bytes in our label values.
47+
mountpoint_len := bytes.Index(fs.Mntonname[:], []byte{0})
48+
mountpoint := string(fs.Mntonname[:mountpoint_len])
4449
if c.ignoredMountPointsPattern.MatchString(mountpoint) {
4550
level.Debug(c.logger).Log("msg", "Ignoring mount point", "mountpoint", mountpoint)
4651
continue
4752
}
4853

49-
device := string(fs.Mntfromname[:])
50-
fstype := string(fs.Fstypename[:])
54+
device_len := bytes.Index(fs.Mntfromname[:], []byte{0})
55+
fstype_len := bytes.Index(fs.Fstypename[:], []byte{0})
56+
device := string(fs.Mntfromname[:device_len])
57+
fstype := string(fs.Fstypename[:fstype_len])
5158
if c.ignoredFSTypesPattern.MatchString(fstype) {
5259
level.Debug(c.logger).Log("msg", "Ignoring fs type", "type", fstype)
5360
continue

0 commit comments

Comments
 (0)