proc: limit maximum time.Time we try to format (#3294)

The loop adding maxAddSeconds to format a time.Time can take multiple
seconds to complete if the time is very far into the future. To avoid
this loop slowing down debugging too much limit it to an arbitrary
maximum.
The chosen maximum is 1000 times the maximum expressible time.Duration,
which is 262 years. This means that we will not format dates beyond
year 262000 AD.
This commit is contained in:
Alessandro Arzilli 2023-03-16 20:12:20 +01:00 committed by GitHub
parent 5b8296782b
commit 9faf66b7a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 1 deletions

@ -5,6 +5,7 @@ import (
"go/constant"
"math"
"reflect"
"os"
"runtime"
"time"
"unsafe"
@ -365,6 +366,7 @@ func main() {
w5 := &W5{nil}
w5.W5 = w5
os.Setenv("TZ", "UTC")
tim1 := time.Unix(233431200, 0)
loc, _ := time.LoadLocation("Mexico/BajaSur")
tim2, _ := time.ParseInLocation("2006-01-02 15:04:05", "2022-06-07 02:03:04", loc)
@ -375,6 +377,7 @@ func main() {
badslice := []int{1, 2, 3}
h := (*reflect.SliceHeader)(unsafe.Pointer(&badslice))
h.Data = 0
tim3 := time.Date(300000, 1, 1, 0, 0, 0, 0, time.Local)
var amb1 = 1
runtime.Breakpoint()
@ -386,5 +389,5 @@ func main() {
longslice := make([]int, 100, 100)
runtime.Breakpoint()
fmt.Println(i1, i2, i3, p1, pp1, amb1, s1, s3, a0, a1, p2, p3, s2, as1, str1, f1, fn1, fn2, nilslice, nilptr, ch1, chnil, m1, mnil, m2, m3, m4, m5, upnil, up1, i4, i5, i6, err1, err2, errnil, iface1, iface2, ifacenil, arr1, parr, cpx1, const1, iface3, iface4, recursive1, recursive1.x, iface5, iface2fn1, iface2fn2, bencharr, benchparr, mapinf, mainMenu, b, b2, sd, anonstruct1, anonstruct2, anoniface1, anonfunc, mapanonstruct1, ifacearr, efacearr, ni8, ni16, ni32, ni64, pinf, ninf, nan, zsvmap, zsslice, zsvar, tm, rettm, errtypednil, emptyslice, emptymap, byteslice, bytestypeslice, runeslice, bytearray, bytetypearray, runearray, longstr, nilstruct, as2, as2.NonPointerRecieverMethod, s4, iface2map, issue1578, ll, unread, w2, w3, w4, w5, longarr, longslice, val, m6, m7, cl, tim1, tim2, typedstringvar, namedA1, namedA2, astructName1(namedA2), badslice)
fmt.Println(i1, i2, i3, p1, pp1, amb1, s1, s3, a0, a1, p2, p3, s2, as1, str1, f1, fn1, fn2, nilslice, nilptr, ch1, chnil, m1, mnil, m2, m3, m4, m5, upnil, up1, i4, i5, i6, err1, err2, errnil, iface1, iface2, ifacenil, arr1, parr, cpx1, const1, iface3, iface4, recursive1, recursive1.x, iface5, iface2fn1, iface2fn2, bencharr, benchparr, mapinf, mainMenu, b, b2, sd, anonstruct1, anonstruct2, anoniface1, anonfunc, mapanonstruct1, ifacearr, efacearr, ni8, ni16, ni32, ni64, pinf, ninf, nan, zsvmap, zsslice, zsvar, tm, rettm, errtypednil, emptyslice, emptymap, byteslice, bytestypeslice, runeslice, bytearray, bytetypearray, runearray, longstr, nilstruct, as2, as2.NonPointerRecieverMethod, s4, iface2map, issue1578, ll, unread, w2, w3, w4, w5, longarr, longslice, val, m6, m7, cl, tim1, tim2, typedstringvar, namedA1, namedA2, astructName1(namedA2), badslice, tim3)
}

@ -2595,6 +2595,10 @@ func (v *Variable) formatTime() {
} else {
// the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext
var t time.Time
if ext > int64(maxAddSeconds/time.Second)*1000 {
// avoid doing the add loop below if it will take too much time
return
}
for ext > int64(maxAddSeconds/time.Second) {
t = t.Add(maxAddSeconds)
ext -= int64(maxAddSeconds / time.Second)

@ -795,6 +795,7 @@ func TestEvalExpression(t *testing.T) {
// pretty printing special types
{"tim1", false, `time.Time(1977-05-25T18:00:00Z)…`, `time.Time(1977-05-25T18:00:00Z)…`, "time.Time", nil},
{"tim2", false, `time.Time(2022-06-07T02:03:04-06:00)…`, `time.Time(2022-06-07T02:03:04-06:00)…`, "time.Time", nil},
{"tim3", false, `time.Time {`, `time.Time {`, "time.Time", nil},
// issue #3034 - map access with long string key
{`m6["very long string 0123456789a0123456789b0123456789c0123456789d0123456789e0123456789f0123456789g012345678h90123456789i0123456789j0123456789"]`, false, `123`, `123`, "int", nil},