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

Respect errors returned on 'json.Marshal()' to prevent unexpected panic #19

Merged
merged 1 commit into from Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,2 +1,4 @@
.coverprofile
/bin
# GoLand
.idea
15 changes: 12 additions & 3 deletions log/logger.go
Expand Up @@ -61,15 +61,24 @@ type LineJSONDev struct {
func (l Line) MarshalJSON() ([]byte, error) {
lineTemp := LineJSON(l)

lineValue, _ := json.Marshal(lineTemp)
lineValue, err := json.Marshal(lineTemp)
if err != nil {
return nil, err
}
verbosity, errConvert := strconv.Atoi(l.Verbosity)
if verbosity > 1 && errConvert == nil {
lineTempDev := LineJSONDev(l)
lineValue, _ = json.Marshal(lineTempDev)
lineValue, err = json.Marshal(lineTempDev)
if err != nil {
return nil, err
}
}
lineValue = lineValue[1 : len(lineValue)-1]

contextValue, _ := json.Marshal(lineTemp.Context)
contextValue, err := json.Marshal(lineTemp.Context)
if err != nil {
return nil, err
}
contextValue = contextValue[1 : len(contextValue)-1]

sep := ""
Expand Down
37 changes: 37 additions & 0 deletions log/logger_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"reflect"
"testing"

Expand Down Expand Up @@ -44,6 +45,42 @@ func TestLogger_Error_noKeysAndValues(t *testing.T) {
require.EqualValues(t, err, logs[0].Error)
}

var unsupportedValues = []float64{
math.NaN(),
math.Inf(-1),
math.Inf(1),
}

func TestLogger_Info_UnsupportedValues(t *testing.T) {
for _, unsupportedValue := range unsupportedValues {
buf := bytes.NewBuffer(nil)
logger := log.NewLogger("", buf, 0, log.JSONEncoder{})
logger.Info("Test unsupported value", "value", unsupportedValue)

if buf.Len() == 0 {
t.Fatal("expected log output, but buffer was empty")
}
assert.Contains(t, string(buf.Bytes()), "failed to encode message")
assert.Contains(t, string(buf.Bytes()), fmt.Sprintf("json: unsupported value: %f", unsupportedValue))
}
}

func TestLogger_Error_UnsupportedValues(t *testing.T) {
for _, unsupportedValue := range unsupportedValues {
buf := bytes.NewBuffer(nil)
logger := log.NewLogger("", buf, 0, log.JSONEncoder{})
err := kverrors.New("an error")
logger.Error(err, "Test unsupported value", "key", unsupportedValue)

if buf.Len() == 0 {
t.Fatal("expected log output, but buffer was empty")
}
println(string(buf.Bytes()))
assert.Contains(t, string(buf.Bytes()), "failed to encode message")
assert.Contains(t, string(buf.Bytes()), fmt.Sprintf("json: unsupported value: %f", unsupportedValue))
}
}

func TestLogger_Error_KeysAndValues(t *testing.T) {
obs, logger := NewObservedLogger()

Expand Down