Skip to content

Commit

Permalink
Fix panic that occurs when an unexported time.Time is rendered.
Browse files Browse the repository at this point in the history
I've also added tests for each of the filter types introduced in v0.3.4 to
ensure they all work correctly when applied to unexported fields.
  • Loading branch information
jmalloc committed Nov 6, 2019
1 parent fb2f944 commit 22e69a6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
33 changes: 33 additions & 0 deletions sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ func TestPrinter_SyncFilter(t *testing.T) {
)
w.Unlock()

test(
t,
"sync.Mutex (unexported struct field)",
struct {
w sync.Mutex
}{},
"{",
" w: sync.Mutex(<unlocked>)",
"}",
)

var rw sync.RWMutex

test(
Expand Down Expand Up @@ -75,6 +86,17 @@ func TestPrinter_SyncFilter(t *testing.T) {
<-barrier
rw.Unlock()

test(
t,
"sync.RWMutex (unexported struct field)",
struct {
rw sync.RWMutex
}{},
"{",
" rw: sync.RWMutex(<unlocked>)",
"}",
)

var o sync.Once
test(
t,
Expand Down Expand Up @@ -106,4 +128,15 @@ func TestPrinter_SyncFilter(t *testing.T) {
" once: <pending>",
"}",
)

test(
t,
"sync.Once (unexported struct field)",
struct {
o sync.Once
}{},
"{",
" o: sync.Once(<pending>)",
"}",
)
}
16 changes: 8 additions & 8 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ var (
func TimeFilter(w io.Writer, v Value) (n int, err error) {
defer must.Recover(&err)

if v.DynamicType != timeType {
return 0, nil
if v.DynamicType == timeType {
mv, ok := unsafereflect.MakeMutable(v.Value)
if ok {
s := mv.Interface().(time.Time).Format(time.RFC3339Nano)
n += must.WriteString(w, s)
return
}
}

s := v.Value.
Convert(timeType).
Interface().(time.Time).
Format(time.RFC3339Nano)

return must.WriteString(w, s), nil
return 0, nil
}

// DurationFilter is a filter that formats time.Duration values.
Expand Down
24 changes: 23 additions & 1 deletion time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,20 @@ func TestPrinter_TimeFilter(t *testing.T) {
tm,
"2019-11-03T10:13:08.839511Z",
)

test(
t,
"time.Time (unexported struct field)",
struct {
t time.Time
}{tm},
"{",
" t: 2019-11-03T10:13:08.839511Z",
"}",
)
}

func TestPrinter_TimeDuration(t *testing.T) {
func TestPrinter_DurationFilter(t *testing.T) {
dur := 20 * time.Second

test(
Expand All @@ -34,4 +45,15 @@ func TestPrinter_TimeDuration(t *testing.T) {
dur,
"20s",
)

test(
t,
"time.Duration (unexported struct field)",
struct {
d time.Duration
}{dur},
"{",
" d: 20s",
"}",
)
}

0 comments on commit 22e69a6

Please sign in to comment.