From b5abd6d1273cfd109bbcdf26f4d38c11168c2ba1 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Sat, 25 Apr 2020 19:31:57 +0200 Subject: [PATCH] log: use strconv conversion instead of custom escape function --- log/format.go | 50 +++++++++--------------------------------- signer/core/cliui.go | 3 +-- signer/fourbyte/abi.go | 2 +- 3 files changed, 12 insertions(+), 43 deletions(-) diff --git a/log/format.go b/log/format.go index a1b5dac629..15d1fd3551 100644 --- a/log/format.go +++ b/log/format.go @@ -325,7 +325,7 @@ func formatJSONValue(value interface{}) interface{} { // formatValue formats a value for serialization func formatLogfmtValue(value interface{}, term bool) string { - if value == nil { + if value == nil || (reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil()) { return "nil" } @@ -358,49 +358,19 @@ func formatLogfmtValue(value interface{}, term bool) string { } } -var stringBufPool = sync.Pool{ - New: func() interface{} { return new(bytes.Buffer) }, -} - +// escapeString checks if the provided string needs escaping/quoting, and +// calls strconv.Quote if needed func escapeString(s string) string { - needsQuotes := false - needsEscape := false + needsQuoting := false for _, r := range s { - if r <= ' ' || r == '=' || r == '"' { - needsQuotes = true - } - if r == '\\' || r == '"' || r == '\n' || r == '\r' || r == '\t' { - needsEscape = true + // We quote everything below " (0x34) and above~ (0x7E), plus equal-sign + if r <= '"' || r > '~' || r == '=' { + needsQuoting = true + break } } - if !needsEscape && !needsQuotes { + if !needsQuoting { return s } - e := stringBufPool.Get().(*bytes.Buffer) - e.WriteByte('"') - for _, r := range s { - switch r { - case '\\', '"': - e.WriteByte('\\') - e.WriteByte(byte(r)) - case '\n': - e.WriteString("\\n") - case '\r': - e.WriteString("\\r") - case '\t': - e.WriteString("\\t") - default: - e.WriteRune(r) - } - } - e.WriteByte('"') - var ret string - if needsQuotes { - ret = e.String() - } else { - ret = string(e.Bytes()[1 : e.Len()-1]) - } - e.Reset() - stringBufPool.Put(e) - return ret + return strconv.Quote(s) } diff --git a/signer/core/cliui.go b/signer/core/cliui.go index 4763cbca8e..65114ac4b6 100644 --- a/signer/core/cliui.go +++ b/signer/core/cliui.go @@ -88,8 +88,7 @@ func (ui *CommandlineUI) confirm() bool { // sanitize quotes and truncates 'txt' if longer than 'limit'. If truncated, // and ellipsis is added after the quoted string func sanitize(txt string, limit int) string { - size := len(txt) - if size > limit { + if len(txt) > limit { return fmt.Sprintf("%q...", txt[:limit]) } return fmt.Sprintf("%q", txt) diff --git a/signer/fourbyte/abi.go b/signer/fourbyte/abi.go index 78428d4cc8..d8fbabd3b1 100644 --- a/signer/fourbyte/abi.go +++ b/signer/fourbyte/abi.go @@ -98,7 +98,7 @@ func parseSelector(unescapedSelector string) ([]byte, error) { // Validate the unescapedSelector and extract it's components groups := selectorRegexp.FindStringSubmatch(unescapedSelector) if len(groups) != 3 { - return nil, fmt.Errorf("invalid unescapedSelector %q (%v matches)", unescapedSelector, len(groups)) + return nil, fmt.Errorf("invalid selector %q (%v matches)", unescapedSelector, len(groups)) } name := groups[1] args := groups[2]