Skip to content

Commit

Permalink
Ensure do not send a null char
Browse files Browse the repository at this point in the history
  • Loading branch information
black-desk authored and jsouthworth committed Aug 11, 2021
1 parent 10b10a4 commit cc14158
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"io"
"reflect"
"strings"
"unicode/utf8"
)

Expand Down Expand Up @@ -116,12 +117,16 @@ func (enc *encoder) encode(v reflect.Value, depth int) {
enc.binwrite(v.Float())
enc.pos += 8
case reflect.String:
if !utf8.Valid([]byte(v.String())) {
str := v.String()
if !utf8.ValidString(str) {
panic(FormatError("input has a not-utf8 char in string"))
}
enc.encode(reflect.ValueOf(uint32(len(v.String()))), depth)
if strings.IndexByte(str, byte(0)) != -1 {
panic(FormatError("input has a null char('\\000') in string"))
}
enc.encode(reflect.ValueOf(uint32(len(str))), depth)
b := make([]byte, v.Len()+1)
copy(b, v.String())
copy(b, str)
b[len(b)-1] = 0
n, err := enc.out.Write(b)
if err != nil {
Expand Down

0 comments on commit cc14158

Please sign in to comment.