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

Decode: don't panic when date time is missing timezone #614

Merged
merged 4 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 4 additions & 10 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package toml
import (
"fmt"
"math"
"regexp"
"strconv"
"time"
)
Expand Down Expand Up @@ -149,21 +150,14 @@ func parseLocalTime(b []byte) (LocalTime, []byte, error) {
t LocalTime
)

const localTimeByteLen = 8
if len(b) < localTimeByteLen {
// check if b matches to have expected format HH:MM:SS[.NNNNNN]
matched, err := regexp.Match(`\d{2}:\d{2}:\d{2}.*`, b)
if err != nil || !matched {
return t, nil, newDecodeError(b, "times are expected to have the format HH:MM:SS[.NNNNNN]")
}

t.Hour = parseDecimalDigits(b[0:2])
if b[2] != ':' {
return t, nil, newDecodeError(b[2:3], "expecting colon between hours and minutes")
}

t.Minute = parseDecimalDigits(b[3:5])
if b[5] != ':' {
return t, nil, newDecodeError(b[5:6], "expecting colon between minutes and seconds")
}

t.Second = parseDecimalDigits(b[6:8])

const minLengthWithFrac = 9
Expand Down
9 changes: 7 additions & 2 deletions unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1885,12 +1885,17 @@ world'`,
{
desc: "bad char between hours and minutes",
data: `a = 2021-03-30 213:1:00`,
msg: `expecting colon between hours and minutes`,
msg: `times are expected to have the format HH:MM:SS[.NNNNNN]`,
},
{
desc: "bad char between minutes and seconds",
data: `a = 2021-03-30 21:312:0`,
msg: `expecting colon between minutes and seconds`,
msg: `times are expected to have the format HH:MM:SS[.NNNNNN]`,
},
{
desc: "invalid time part",
data: `a=1979-05-27T90:+2:99`,
msg: `times are expected to have the format HH:MM:SS[.NNNNNN]`,
},
{
desc: `binary with invalid digit`,
Expand Down