Skip to content

Fix greedy regex causing time.Time #123

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

Merged
merged 1 commit into from
Nov 3, 2023
Merged

Fix greedy regex causing time.Time #123

merged 1 commit into from
Nov 3, 2023

Conversation

ejcx
Copy link
Contributor

@ejcx ejcx commented Nov 3, 2023

The code in question is this small two lines.

if (/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)/.test(val))
  return "time.Time";

But what happens if the string in the object contains a timestamp and a bunch of other junk meaning it isn't a time at all, but rather just a string. There's a lot of valid use cases for this. For example, parsing syslog...or something.

let val = "2023-11-03T02:18:10+00:00 my name is bob"
if (/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)/.test(val)) {
    console.log("time.Time");
}
> time.Time

Oh no. It also seems to function in this way when the extra junk is on the other side of the timestamp.

let val = "my name is bob 2023-11-03T02:18:10+00:00"
if (/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)/.test(val)) {
    console.log("time.Time");
}
> time.Time

That's no fun. This is easy to overlook, and luckily it's a simple fix.

let val = "2023-11-03T02:18:10+00:00 my name is bob"
if (/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)$/.test(val)) {
    console.log("time.Time");
} else {
    console.log("Fixed")
}
> Fixed

Reference #122

The code in question is this small two lines.

if (/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)/.test(val))
  return "time.Time";

But what happens if the string in the object contains a timestamp and a
bunch of other junk meaning it isn't a time at all, but rather just a
string. There's a lot of valid use cases for this. For example, parsing
syslog...or something.

let val = "2023-11-03T02:18:10+00:00 my name is bob"
if (/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)/.test(val)) {
    console.log("time.Time");
}
> time.Time

Oh no. It also seems to function in this way when the extra junk is on
the other side of the timestamp.

let val = "my name is bob 2023-11-03T02:18:10+00:00"
if (/\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)/.test(val)) {
    console.log("time.Time");
}
> time.Time

That's no fun. This is easy to overlook, and luckily it's a simple fix.

let val = "2023-11-03T02:18:10+00:00 my name is bob"
if (/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(\+\d\d:\d\d|Z)$/.test(val)) {
    console.log("time.Time");
} else {
    console.log("Fixed")
}
> Fixed
Copy link
Owner

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Ooooops, good catch -- thanks for the patch!

@mholt mholt merged commit 086b5b2 into mholt:master Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants