Skip to content

Commit a9a71ce

Browse files
committed
apply review feedback
1 parent 52e29b1 commit a9a71ce

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

decode.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package logfmt
22

33
import (
44
"bufio"
5+
"bytes"
56
"fmt"
67
"io"
8+
"unicode/utf8"
79
)
810

911
// A Decoder reads and decodes logfmt records from an input stream.
@@ -70,14 +72,14 @@ func (dec *Decoder) ScanKeyval() bool {
7072
key:
7173
const invalidKeyError = "invalid key"
7274

73-
start := dec.pos
75+
start, multibyte := dec.pos, false
7476
for p, c := range line[dec.pos:] {
7577
switch {
7678
case c == '=':
7779
dec.pos += p
7880
if dec.pos > start {
7981
dec.key = line[start:dec.pos]
80-
if invalidKey(dec.key) {
82+
if multibyte && bytes.IndexRune(dec.key, utf8.RuneError) != -1 {
8183
dec.syntaxError(invalidKeyError)
8284
return false
8385
}
@@ -95,18 +97,20 @@ key:
9597
dec.pos += p
9698
if dec.pos > start {
9799
dec.key = line[start:dec.pos]
98-
if invalidKey(dec.key) {
100+
if multibyte && bytes.IndexRune(dec.key, utf8.RuneError) != -1 {
99101
dec.syntaxError(invalidKeyError)
100102
return false
101103
}
102104
}
103105
return true
106+
case c >= utf8.RuneSelf:
107+
multibyte = true
104108
}
105109
}
106110
dec.pos = len(line)
107111
if dec.pos > start {
108112
dec.key = line[start:dec.pos]
109-
if invalidKey(dec.key) {
113+
if multibyte && bytes.IndexRune(dec.key, utf8.RuneError) != -1 {
110114
dec.syntaxError(invalidKeyError)
111115
return false
112116
}
@@ -200,9 +204,6 @@ func (dec *Decoder) Value() []byte {
200204
return dec.value
201205
}
202206

203-
// func (dec *Decoder) DecodeValue() ([]byte, error) {
204-
// }
205-
206207
// Err returns the first non-EOF error that was encountered by the Scanner.
207208
func (dec *Decoder) Err() error {
208209
return dec.err

decode_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func TestDecoder_errors(t *testing.T) {
120120
{"a=\"\\u1\"", &SyntaxError{Msg: "invalid quoted value", Line: 1, Pos: 8}},
121121
{"a\ufffd=bar", &SyntaxError{Msg: "invalid key", Line: 1, Pos: 5}},
122122
{"\x80=bar", &SyntaxError{Msg: "invalid key", Line: 1, Pos: 2}},
123+
{"\x80", &SyntaxError{Msg: "invalid key", Line: 1, Pos: 2}},
123124
}
124125

125126
for _, test := range tests {

encode.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ func invalidKeyRune(r rune) bool {
170170
}
171171

172172
func invalidKeyString(key string) bool {
173-
return len(key) == 0 || strings.IndexFunc(key, invalidKeyRune) != -1 || !utf8.ValidString(key)
173+
return len(key) == 0 || strings.IndexFunc(key, invalidKeyRune) != -1
174174
}
175175

176176
func invalidKey(key []byte) bool {
177-
return len(key) == 0 || bytes.IndexFunc(key, invalidKeyRune) != -1 || !utf8.Valid(key)
177+
return len(key) == 0 || bytes.IndexFunc(key, invalidKeyRune) != -1
178178
}
179179

180180
func writeStringKey(w io.Writer, key string) error {
@@ -239,7 +239,7 @@ func writeStringValue(w io.Writer, value string, ok bool) error {
239239
var err error
240240
if ok && value == "null" {
241241
_, err = io.WriteString(w, `"null"`)
242-
} else if strings.IndexFunc(value, needsQuotedValueRune) != -1 || !utf8.ValidString(value) {
242+
} else if strings.IndexFunc(value, needsQuotedValueRune) != -1 {
243243
_, err = writeQuotedString(w, value)
244244
} else {
245245
_, err = io.WriteString(w, value)
@@ -249,7 +249,7 @@ func writeStringValue(w io.Writer, value string, ok bool) error {
249249

250250
func writeBytesValue(w io.Writer, value []byte) error {
251251
var err error
252-
if bytes.IndexFunc(value, needsQuotedValueRune) >= 0 || !utf8.Valid(value) {
252+
if bytes.IndexFunc(value, needsQuotedValueRune) != -1 {
253253
_, err = writeQuotedBytes(w, value)
254254
} else {
255255
_, err = w.Write(value)

encode_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ func TestEncodeKeyval(t *testing.T) {
5353
{key: decimalStringer{5, 9}, value: "v", want: "5.9=v"},
5454
{key: (*decimalStringer)(nil), value: "v", err: logfmt.ErrNilKey},
5555
{key: marshalerStringer{5, 9}, value: "v", want: "5.9=v"},
56-
{key: "k", value: "\xbd", want: "k=\"\\ufffd\""},
57-
{key: "k", value: "\ufffd\x00", want: "k=\"\\ufffd\\u0000\""},
58-
{key: "k", value: "\ufffd", want: "k=\"\\ufffd\""},
59-
{key: "k", value: []byte("\ufffd\x00"), want: "k=\"\\ufffd\\u0000\""},
60-
{key: "k", value: []byte("\ufffd"), want: "k=\"\\ufffd\""},
56+
{key: "k", value: "\xbd", want: `k="\ufffd"`},
57+
{key: "k", value: "\ufffd\x00", want: `k="\ufffd\u0000"`},
58+
{key: "k", value: "\ufffd", want: `k="\ufffd"`},
59+
{key: "k", value: []byte("\ufffd\x00"), want: `k="\ufffd\u0000"`},
60+
{key: "k", value: []byte("\ufffd"), want: `k="\ufffd"`},
6161
}
6262

6363
for _, d := range data {

fuzz.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func parse(data []byte) ([][]kv, error) {
7373
kvs = append(kvs, kv{dec.Key(), dec.Value()})
7474
}
7575
got = append(got, kvs)
76-
kvs = nil
7776
}
7877
return got, dec.Err()
7978
}

0 commit comments

Comments
 (0)