Skip to content

Commit

Permalink
msgpack: fixstr length field is 5 bits
Browse files Browse the repository at this point in the history
Fixes #634
  • Loading branch information
wader committed Apr 8, 2023
1 parent b9864fc commit 97360d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions format/msgpack/msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func decodeMsgPackValue(d *decode.D) {
{r: [2]byte{0x80, 0x8f}, s: scalar.Uint{Sym: "fixmap"}, d: mapFn(-4, 4)},
{r: [2]byte{0x90, 0x9f}, s: scalar.Uint{Sym: "fixarray"}, d: arrayFn(-4, 4)},
{r: [2]byte{0xa0, 0xbf}, s: scalar.Uint{Sym: "fixstr"}, d: func(d *decode.D) {
d.SeekRel(-4)
length := d.FieldU4("length")
d.SeekRel(-5)
length := d.FieldU5("length")
d.FieldUTF8("value", int(length))
}},
{r: [2]byte{0xc0, 0xc0}, s: scalar.Uint{Sym: "nil"}, d: func(d *decode.D) {
Expand Down
8 changes: 8 additions & 0 deletions format/msgpack/testdata/fixstr.fqtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Msgpack: fixstr gets wrong length
# https://github.com/wader/fq/issues/634
$ fq -n '"b3737472696e67206f66206c656e677468203139" | from_hex | msgpack | dv'
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: (msgpack) 0x0-0x13.7 (20)
0x00|b3 |. | type: "fixstr" (0xb3) 0x0-0x0.7 (1)
0x00|b3 |. | length: 19 0x0.3-0x0.7 (0.5)
0x00| 73 74 72 69 6e 67 20 6f 66 20 6c 65 6e 67 74| string of lengt| value: "string of length 19" 0x1-0x13.7 (19)
0x10|68 20 31 39| |h 19| |
20 changes: 10 additions & 10 deletions format/msgpack/testdata/test.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $ fq -d msgpack dv test.msgpack
| | | [0]{}: pair 0x1-0xa.7 (10)
| | | key{}: 0x1-0x6.7 (6)
0x00| a5 | . | type: "fixstr" (0xa5) 0x1-0x1.7 (1)
0x00| a5 | . | length: 5 0x1.4-0x1.7 (0.4)
0x00| a5 | . | length: 5 0x1.3-0x1.7 (0.5)
0x00| 61 72 72 61 79 | array | value: "array" 0x2-0x6.7 (5)
| | | value{}: 0x7-0xa.7 (4)
0x00| 93 | . | type: "fixarray" (0x93) 0x7-0x7.7 (1)
Expand All @@ -26,7 +26,7 @@ $ fq -d msgpack dv test.msgpack
| | | [1]{}: pair 0xb-0x1c.7 (18)
| | | key{}: 0xb-0x11.7 (7)
0x00| a6 | . | type: "fixstr" (0xa6) 0xb-0xb.7 (1)
0x00| a6 | . | length: 6 0xb.4-0xb.7 (0.4)
0x00| a6 | . | length: 6 0xb.3-0xb.7 (0.5)
0x00| 6f 62 6a 65| obje| value: "object" 0xc-0x11.7 (6)
0x10|63 74 |ct |
| | | value{}: 0x12-0x1c.7 (11)
Expand All @@ -36,16 +36,16 @@ $ fq -d msgpack dv test.msgpack
| | | [0]{}: pair 0x13-0x1c.7 (10)
| | | key{}: 0x13-0x16.7 (4)
0x10| a3 | . | type: "fixstr" (0xa3) 0x13-0x13.7 (1)
0x10| a3 | . | length: 3 0x13.4-0x13.7 (0.4)
0x10| a3 | . | length: 3 0x13.3-0x13.7 (0.5)
0x10| 6b 65 79 | key | value: "key" 0x14-0x16.7 (3)
| | | value{}: 0x17-0x1c.7 (6)
0x10| a5 | . | type: "fixstr" (0xa5) 0x17-0x17.7 (1)
0x10| a5 | . | length: 5 0x17.4-0x17.7 (0.4)
0x10| a5 | . | length: 5 0x17.3-0x17.7 (0.5)
0x10| 76 61 6c 75 65 | value | value: "value" 0x18-0x1c.7 (5)
| | | [2]{}: pair 0x1d-0x24.7 (8)
| | | key{}: 0x1d-0x23.7 (7)
0x10| a6 | . | type: "fixstr" (0xa6) 0x1d-0x1d.7 (1)
0x10| a6 | . | length: 6 0x1d.4-0x1d.7 (0.4)
0x10| a6 | . | length: 6 0x1d.3-0x1d.7 (0.5)
0x10| 6e 75| nu| value: "number" 0x1e-0x23.7 (6)
0x20|6d 62 65 72 |mber |
| | | value{}: 0x24-0x24.7 (1)
Expand All @@ -54,32 +54,32 @@ $ fq -d msgpack dv test.msgpack
| | | [3]{}: pair 0x25-0x2f.7 (11)
| | | key{}: 0x25-0x2b.7 (7)
0x20| a6 | . | type: "fixstr" (0xa6) 0x25-0x25.7 (1)
0x20| a6 | . | length: 6 0x25.4-0x25.7 (0.4)
0x20| a6 | . | length: 6 0x25.3-0x25.7 (0.5)
0x20| 73 74 72 69 6e 67 | string | value: "string" 0x26-0x2b.7 (6)
| | | value{}: 0x2c-0x2f.7 (4)
0x20| a3 | . | type: "fixstr" (0xa3) 0x2c-0x2c.7 (1)
0x20| a3 | . | length: 3 0x2c.4-0x2c.7 (0.4)
0x20| a3 | . | length: 3 0x2c.3-0x2c.7 (0.5)
0x20| 61 62 63| abc| value: "abc" 0x2d-0x2f.7 (3)
| | | [4]{}: pair 0x30-0x35.7 (6)
| | | key{}: 0x30-0x34.7 (5)
0x30|a4 |. | type: "fixstr" (0xa4) 0x30-0x30.7 (1)
0x30|a4 |. | length: 4 0x30.4-0x30.7 (0.4)
0x30|a4 |. | length: 4 0x30.3-0x30.7 (0.5)
0x30| 74 72 75 65 | true | value: "true" 0x31-0x34.7 (4)
| | | value{}: 0x35-0x35.7 (1)
0x30| c3 | . | type: "true" (0xc3) 0x35-0x35.7 (1)
| | | value: true 0x36-NA (0)
| | | [5]{}: pair 0x36-0x3c.7 (7)
| | | key{}: 0x36-0x3b.7 (6)
0x30| a5 | . | type: "fixstr" (0xa5) 0x36-0x36.7 (1)
0x30| a5 | . | length: 5 0x36.4-0x36.7 (0.4)
0x30| a5 | . | length: 5 0x36.3-0x36.7 (0.5)
0x30| 66 61 6c 73 65 | false | value: "false" 0x37-0x3b.7 (5)
| | | value{}: 0x3c-0x3c.7 (1)
0x30| c2 | . | type: "false" (0xc2) 0x3c-0x3c.7 (1)
| | | value: false 0x3d-NA (0)
| | | [6]{}: pair 0x3d-0x42.7 (6)
| | | key{}: 0x3d-0x41.7 (5)
0x30| a4 | . | type: "fixstr" (0xa4) 0x3d-0x3d.7 (1)
0x30| a4 | . | length: 4 0x3d.4-0x3d.7 (0.4)
0x30| a4 | . | length: 4 0x3d.3-0x3d.7 (0.5)
0x30| 6e 75| nu| value: "null" 0x3e-0x41.7 (4)
0x40|6c 6c |ll |
| | | value{}: 0x42-0x42.7 (1)
Expand Down

0 comments on commit 97360d6

Please sign in to comment.