fs: reject --bwlimit times without a colon or with signed digits

validateHour only checked the length of the hh:mm field and then
parsed the two halves with strconv.Atoi. It never checked the third
character, so "10h20" or "Mon-10.20" were accepted as 10:20, and
since Atoi accepts a sign, "+1:00" and "Mon-10:+5" were accepted
as 01:00 and 10:05.

Require the colon and four digits so these are reported as invalid
time specifications like other malformed times.
This commit is contained in:
kevin9327 authored and Nick Craig-Wood committed 2026-10-31 12:36:13 +00:00
1 parent 18c7de0920
commit 64a68c7380
2 files changed
+11 -1

No files matched your search

+7 -1
View File
@@ -82,9 +82,15 @@ func (x BwTimetable) String() string {
// Basic hour format checking
func validateHour(HHMM string) error {
if len(HHMM) != 5 {
if len(HHMM) != 5 || HHMM[2] != ':' {
return fmt.Errorf("invalid time specification (hh:mm): %q", HHMM)
}
// strconv.Atoi accepts a sign, so check for digits explicitly
for _, i := range []int{0, 1, 3, 4} {
if HHMM[i] < '0' || HHMM[i] > '9' {
return fmt.Errorf("invalid time specification (hh:mm): %q", HHMM)
}
}
hh, err := strconv.Atoi(HHMM[0:2])
if err != nil {
return fmt.Errorf("invalid hour in time specification %q: %v", HHMM, err)
+4
View File
@@ -32,6 +32,10 @@ func TestBwTimetableSet(t *testing.T) {
{"bad-10:20,666", BwTimetable{}, true, ""},
{"Mon-bad,666", BwTimetable{}, true, ""},
{"Mon-10:20,bad", BwTimetable{}, true, ""},
{"10h20,666", BwTimetable{}, true, ""},
{"Mon-10.20,666", BwTimetable{}, true, ""},
{"+1:00,666", BwTimetable{}, true, ""},
{"Mon-10:+5,666", BwTimetable{}, true, ""},
{
"0",
BwTimetable{