From e6bdd9866787e191a24fce3bdd5c0088768beb1e Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Tue, 19 Mar 2024 20:50:00 +0530 Subject: [PATCH] util: throw if file has !digit characters Throw if file has non-numeric data, in addition to hinting the path that caused the error. Fixes: prometheus#304, prometheus/node_exporter#1710 Signed-off-by: Pranshu Srivastava --- internal/util/parse.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/util/parse.go b/internal/util/parse.go index 14272dc7..92648ed1 100644 --- a/internal/util/parse.go +++ b/internal/util/parse.go @@ -14,9 +14,11 @@ package util import ( + "fmt" "os" "strconv" "strings" + "unicode" ) // ParseUint32s parses a slice of strings into a slice of uint32s. @@ -85,6 +87,11 @@ func ReadUintFromFile(path string) (uint64, error) { if err != nil { return 0, err } + for _, c := range data { + if !unicode.IsDigit(rune(c)) { + return 0, fmt.Errorf("%w: %s", strconv.ErrSyntax, path) + } + } return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64) }