Skip to content

Commit

Permalink
Merge pull request #15 from jmank88/empty-string
Browse files Browse the repository at this point in the history
support empty string
  • Loading branch information
jmank88 authored Aug 16, 2019
2 parents b17dce8 + fd2c813 commit 4fd8cac
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: go

go:
- 1.9.x
- "1.10"
- 1.11.x
- 1.12.x
- tip

install:
Expand Down
16 changes: 10 additions & 6 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,12 @@ func (r *binaryReader) readString(max int) (string, error) {
if err != nil {
return "", errors.Wrap(err, "failed to read string length prefix")
}
if l < 1 {
switch {
case l == 0:
return "", nil
case l < 0:
return "", errors.Errorf("illegal string length prefix: %d", l)
}
if l > max {
case l > max:
return "", errors.Errorf("string length prefix exceeds max allocation limit of %d: %d", max, l)
}
b := make([]byte, l)
Expand Down Expand Up @@ -376,10 +378,12 @@ func (r *blockReader) readString(max int) (string, error) {
if err != nil {
return "", errors.Wrap(err, "failed to read string length prefix")
}
if l < 1 {
switch {
case l == 0:
return "", nil
case l < 0:
return "", errors.Errorf("illegal string length prefix: %d", l)
}
if l > max {
case l > max:
return "", errors.Errorf("string length prefix exceeds max allocation limit of %d: %d", max, l)
}
s, err := r.nextBlock()
Expand Down
1 change: 1 addition & 0 deletions ubjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var cases = map[string]testCase{
"C=a": {Char('a'), []byte{'C', 0x61}, "[C][a]"},

"string=string": {"string", append([]byte{'S', 0x55, 0x06}, "string"...), "[S][U][6][string]"},
"string=empty": {"", []byte{'S', 0x55, 0x00}, "[S][U][0]"},

"Array-empty": {[0]int{}, []byte{0x5b, 0x23, 0x55, 0x0}, "[[][#][U][0]"},

Expand Down

0 comments on commit 4fd8cac

Please sign in to comment.