Skip to content

Commit

Permalink
util: throw if file has !digit characters
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rexagod committed Mar 19, 2024
1 parent dcad38f commit 017b78a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/util/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
package util

import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"unicode"
)

// ParseUint32s parses a slice of strings into a slice of uint32s.
Expand Down Expand Up @@ -70,6 +72,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)
}

Expand Down

0 comments on commit 017b78a

Please sign in to comment.