Skip to content

Commit

Permalink
support empty string; update CI test versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Aug 16, 2019
1 parent b17dce8 commit fc8e4d4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: go

go:
- 1.9.x
- "1.10"
- 1.11.x
- 1.12.x
- "1.13"
- 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 fc8e4d4

Please sign in to comment.