Skip to content

Commit

Permalink
Sanitize strings from /sys/class/power_supply
Browse files Browse the repository at this point in the history
Avoid panic on invalid UTF-8 from /sys/class/power_supply by
sanitizing strings parsed from the kernel.
* Add a broken string to the test fixtures.

Fixes: #1979

Signed-off-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
SuperQ committed Mar 2, 2021
1 parent e80863f commit d197a2f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* [ENHANCEMENT]
* [BUGFIX]

* [BUGFIX] Sanitize strings from /sys/class/power_supply

## 1.1.1 / 2021-02-12

* [BUGFIX] Fix ineffassign issue #1957
Expand Down
2 changes: 1 addition & 1 deletion collector/fixtures/e2e-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2613,7 +2613,7 @@ node_power_supply_energy_watthour{power_supply="BAT0"} 36.58
# HELP node_power_supply_info info of /sys/class/power_supply/<power_supply>.
# TYPE node_power_supply_info gauge
node_power_supply_info{power_supply="AC",type="Mains"} 1
node_power_supply_info{capacity_level="Normal",manufacturer="LGC",model_name="LNV-45N1",power_supply="BAT0",serial_number="38109",status="Discharging",technology="Li-ion",type="Battery"} 1
node_power_supply_info{capacity_level="Normal",manufacturer="LGC",model_name="LNV-45N1🤬",power_supply="BAT0",serial_number="38109",status="Discharging",technology="Li-ion",type="Battery"} 1
# HELP node_power_supply_online online value of /sys/class/power_supply/<power_supply>.
# TYPE node_power_supply_online gauge
node_power_supply_online{power_supply="AC"} 0
Expand Down
2 changes: 1 addition & 1 deletion collector/fixtures/sys.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/class/power_supply/BAT0/model_name
Lines: 1
LNV-45N1
LNV-45N1��
Mode: 444
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: sys/class/power_supply/BAT0/power
Expand Down
3 changes: 2 additions & 1 deletion collector/powersupplyclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"regexp"
"strings"

"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -153,7 +154,7 @@ func (c *powerSupplyClassCollector) Update(ch chan<- prometheus.Metric) error {
} {
if value != "" {
keys = append(keys, name)
values = append(values, value)
values = append(values, strings.ToValidUTF8(value, "🤬"))
}
}

Expand Down

0 comments on commit d197a2f

Please sign in to comment.