Skip to content

Commit 0990210

Browse files
committed
fix: encode DEL (0x7f) control character (#17)
encode DEL control characters in values, and remove them from keys. Fixes: 3f5ba89
1 parent 3948912 commit 0990210

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func writeValue(w io.Writer, value interface{}) error {
233233
}
234234

235235
func needsQuotedValueRune(r rune) bool {
236-
return r <= ' ' || r == '=' || r == '"' || r == utf8.RuneError
236+
return r <= ' ' || r == '=' || r == '"' || r == 0x7f || r == utf8.RuneError
237237
}
238238

239239
func writeStringValue(w io.Writer, value string, ok bool) error {

encode_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ func TestEncodeKeyval(t *testing.T) {
5656
{key: "k", value: "\xbd", want: `k="\ufffd"`},
5757
{key: "k", value: "\ufffd\x00", want: `k="\ufffd\u0000"`},
5858
{key: "k", value: "\ufffd", want: `k="\ufffd"`},
59+
{key: "k", value: "\u007f", want: `k="\u007f"`},
60+
{key: "k\u007f", value: "v", want: `k=v`},
5961
{key: "k", value: []byte("\ufffd\x00"), want: `k="\ufffd\u0000"`},
6062
{key: "k", value: []byte("\ufffd"), want: `k="\ufffd"`},
6163
{key: "k", value: []byte("\u007f"), want: `k="\u007f"`},
64+
{key: []byte("k\u007f"), value: "v", want: `k=v`},
6265
}
6366

6467
for _, d := range data {

jsonstring.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func writeQuotedString(w io.Writer, s string) (int, error) {
4040
start := 0
4141
for i := 0; i < len(s); {
4242
if b := s[i]; b < utf8.RuneSelf {
43-
if 0x20 <= b && b != '\\' && b != '"' {
43+
if 0x20 <= b && b != '\\' && b != '"' && b != 0x7f {
4444
i++
4545
continue
4646
}
@@ -91,14 +91,14 @@ func writeQuotedString(w io.Writer, s string) (int, error) {
9191
return n, err
9292
}
9393

94-
// NOTE: keep in sync with writeQuoteString above.
94+
// NOTE: keep in sync with writeQuotedString above.
9595
func writeQuotedBytes(w io.Writer, s []byte) (int, error) {
9696
buf := getBuffer()
9797
buf.WriteByte('"')
9898
start := 0
9999
for i := 0; i < len(s); {
100100
if b := s[i]; b < utf8.RuneSelf {
101-
if 0x20 <= b && b != '\\' && b != '"' {
101+
if 0x20 <= b && b != '\\' && b != '"' && b != 0x7f {
102102
i++
103103
continue
104104
}

0 commit comments

Comments
 (0)