Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: throw if file has !digit characters #614

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 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"
"os"
"strconv"
"strings"
"unicode"
)

// ParseUint32s parses a slice of strings into a slice of uint32s.
Expand Down Expand Up @@ -85,6 +87,14 @@ func ReadUintFromFile(path string) (uint64, error) {
if err != nil {
return 0, err
}
for _, c := range data {
if string(c) == "\n" {
continue
}
if !unicode.IsDigit(rune(c)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is going to match a whole bunch of unicode glyph runes that are not [0-9]. I think we should be more strict and check like this:

Suggested change
if !unicode.IsDigit(rune(c)) {
if !strings.ContainsAny(c, "0123456789") {

return 0, fmt.Errorf("%w ('%s'): %s", strconv.ErrSyntax, string(c), path)
}
}
return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
}

Expand Down