From e6bdd9866787e191a24fce3bdd5c0088768beb1e Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Tue, 19 Mar 2024 20:50:00 +0530 Subject: [PATCH 1/2] 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) } From aa1c8356082b421a0689721bdde5ae0a7ea7ce91 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sat, 30 Mar 2024 23:39:29 +0530 Subject: [PATCH 2/2] fixup! util: throw if file has !digit characters Signed-off-by: Pranshu Srivastava --- internal/util/parse.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/util/parse.go b/internal/util/parse.go index 92648ed1..cb4f5257 100644 --- a/internal/util/parse.go +++ b/internal/util/parse.go @@ -88,8 +88,11 @@ func ReadUintFromFile(path string) (uint64, error) { return 0, err } for _, c := range data { + if string(c) == "\n" { + continue + } if !unicode.IsDigit(rune(c)) { - return 0, fmt.Errorf("%w: %s", strconv.ErrSyntax, path) + return 0, fmt.Errorf("%w ('%s'): %s", strconv.ErrSyntax, string(c), path) } } return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)