forked from open-policy-agent/opa
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
topdown: Fixing nanos int overflow issue for time.* built-in functions
The go Time.UnixNano() function result is undefined for dates that cannot fit into an int64 when converted to Unix time in nanoseconds. Updating built-in functions to return error if date is too low/high. Fixes: open-policy-agent#4098 Signed-off-by: Johan Fylling <johan.dev@fylling.se>
- Loading branch information
1 parent
cc4816e
commit fd5327b
Showing
5 changed files
with
150 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,50 @@ | ||
cases: | ||
- data: | ||
a: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
b: | ||
v1: hello | ||
v2: goodbye | ||
c: | ||
- x: | ||
- true | ||
- false | ||
- foo | ||
"y": | ||
- null | ||
- 3.14159 | ||
z: | ||
p: true | ||
q: false | ||
d: | ||
e: | ||
- bar | ||
- baz | ||
f: | ||
- xs: | ||
- 1 | ||
ys: | ||
- 2 | ||
- xs: | ||
- 2 | ||
ys: | ||
- 3 | ||
g: | ||
a: | ||
- 1 | ||
- 0 | ||
- 0 | ||
- 0 | ||
b: | ||
- 0 | ||
- 2 | ||
- 0 | ||
- 0 | ||
c: | ||
- 0 | ||
- 0 | ||
- 0 | ||
- 4 | ||
h: | ||
- - 1 | ||
- 2 | ||
- 3 | ||
- - 2 | ||
- 3 | ||
- 4 | ||
l: | ||
- a: bob | ||
b: -1 | ||
c: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
- a: alice | ||
b: 1 | ||
c: | ||
- 2 | ||
- 3 | ||
- 4 | ||
- 5 | ||
d: null | ||
m: [] | ||
numbers: | ||
- "1" | ||
- "2" | ||
- "3" | ||
- "4" | ||
strings: | ||
bar: 2 | ||
baz: 3 | ||
foo: 1 | ||
three: 3 | ||
- note: time/parse_nanos | ||
modules: | ||
- | | ||
package generated | ||
p = ns { | ||
time.parse_ns("2006-01-02T15:04:05Z07:00", "2017-06-02T19:00:00-07:00", ns) | ||
p[case_id] = ns { | ||
case := input.cases[case_id] | ||
time.parse_ns(case.layout, case.value, ns) | ||
} | ||
note: time/parse nanos | ||
query: data.generated.p = x | ||
input: { cases: { | ||
1: { layout: "2006-01-02T15:04:05Z07:00", value: "2017-06-02T19:00:00-07:00" }, # RFC3339 | ||
2: { layout: "2006-01-02T15:04:05Z07:00", value: "1970-01-01T00:00:00-00:00" }, # Earliest valid time | ||
3: { layout: "2006-01-02T15:04:05Z07:00", value: "2262-04-11T23:47:16.854775807-00:00" }, # Latest valid time | ||
4: { layout: "01/02 03:04:05PM '06 -0700", value: "06/02 07:00:00PM '17 -0700" }, # Layout | ||
5: { layout: "02 Jan 06 15:04 -0700", value: "02 Jun 17 19:00 -0700" }, # RFC822Z | ||
}} | ||
want_result: | ||
- x: 1496455200000000000 | ||
- x: { | ||
1: 1496455200000000000, | ||
2: 0, | ||
3: 9223372036854775807, | ||
4: 1496455200000000000, | ||
5: 1496455200000000000 | ||
} | ||
- note: time/parse_nanos_too_small | ||
modules: | ||
- | | ||
package generated | ||
p = ns { | ||
time.parse_ns("2006-01-02T15:04:05Z07:00", "1969-12-31T23:59:59.999999999-00:00", ns) | ||
} | ||
query: data.generated.p = x | ||
strict_error: true | ||
want_error_code: eval_builtin_error | ||
want_error: 'time is before 1970-01-01 00:00:00 +0000 UTC, and cannot be converted to epoch nano seconds' | ||
- note: time/parse_nanos_too_large | ||
modules: | ||
- | | ||
package generated | ||
p = ns { | ||
time.parse_ns("2006-01-02T15:04:05Z07:00", "2262-04-11T23:47:16.854775808-00:00", ns) | ||
} | ||
query: data.generated.p = x | ||
strict_error: true | ||
want_error_code: eval_builtin_error | ||
want_error: 'time is after 2262-04-11 23:47:16.854775807 +0000 UTC, and cannot be converted to epoch nano seconds' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,46 @@ | ||
cases: | ||
- data: | ||
a: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
b: | ||
v1: hello | ||
v2: goodbye | ||
c: | ||
- x: | ||
- true | ||
- false | ||
- foo | ||
"y": | ||
- null | ||
- 3.14159 | ||
z: | ||
p: true | ||
q: false | ||
d: | ||
e: | ||
- bar | ||
- baz | ||
f: | ||
- xs: | ||
- 1 | ||
ys: | ||
- 2 | ||
- xs: | ||
- 2 | ||
ys: | ||
- 3 | ||
g: | ||
a: | ||
- 1 | ||
- 0 | ||
- 0 | ||
- 0 | ||
b: | ||
- 0 | ||
- 2 | ||
- 0 | ||
- 0 | ||
c: | ||
- 0 | ||
- 0 | ||
- 0 | ||
- 4 | ||
h: | ||
- - 1 | ||
- 2 | ||
- 3 | ||
- - 2 | ||
- 3 | ||
- 4 | ||
l: | ||
- a: bob | ||
b: -1 | ||
c: | ||
- 1 | ||
- 2 | ||
- 3 | ||
- 4 | ||
- a: alice | ||
b: 1 | ||
c: | ||
- 2 | ||
- 3 | ||
- 4 | ||
- 5 | ||
d: null | ||
m: [] | ||
numbers: | ||
- "1" | ||
- "2" | ||
- "3" | ||
- "4" | ||
strings: | ||
bar: 2 | ||
baz: 3 | ||
foo: 1 | ||
three: 3 | ||
- note: time/parse_rfc3339_nanos | ||
modules: | ||
- | | ||
package generated | ||
p = ns { | ||
time.parse_rfc3339_ns("2017-06-02T19:00:00-07:00", ns) | ||
} | ||
note: time/parse rfc3339 nanos | ||
- | | ||
package generated | ||
p[time] = ns { | ||
time = input.cases[_] | ||
time.parse_rfc3339_ns(time, ns) | ||
} | ||
query: data.generated.p = x | ||
input: { cases: [ | ||
"2017-06-02T19:00:00-07:00", | ||
"1970-01-01T00:00:00-00:00", | ||
"2262-04-11T23:47:16.854775807-00:00" | ||
]} | ||
want_result: | ||
- x: 1496455200000000000 | ||
- x: { | ||
"1970-01-01T00:00:00-00:00": 0, | ||
"2017-06-02T19:00:00-07:00": 1496455200000000000, | ||
"2262-04-11T23:47:16.854775807-00:00": 9223372036854775807 | ||
} | ||
- note: time/parse_rfc3339_nanos_too_small | ||
modules: | ||
- | | ||
package generated | ||
p = ns { | ||
time.parse_rfc3339_ns("1969-12-31T23:59:59.999999999-00:00", ns) | ||
} | ||
query: data.generated.p = x | ||
strict_error: true | ||
want_error_code: eval_builtin_error | ||
want_error: 'time is before 1970-01-01 00:00:00 +0000 UTC, and cannot be converted to epoch nano seconds' | ||
- note: time/parse_rfc3339_nanos_too_large | ||
modules: | ||
- | | ||
package generated | ||
p = ns { | ||
time.parse_rfc3339_ns("2262-04-11T23:47:16.854775808-00:00", ns) | ||
} | ||
query: data.generated.p = x | ||
strict_error: true | ||
want_error_code: eval_builtin_error | ||
want_error: 'time is after 2262-04-11 23:47:16.854775807 +0000 UTC, and cannot be converted to epoch nano seconds' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.