Commit 6ce3a6ee authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

encoder: Fix int ranges for which BININT1 and BININT2 are emitted

BININT1 emits unsigned integer that fits into 1 byte.
BININT2 emits unsigned integer that fits into 2 bytes.

So they cover [0, 0xff] and [0, 0xffff] ranges including edges. However
we were emitting BININTX opcodes only for numbers being strictly inside
those ranges - i.e. (0, 0xff) and (0, 0xffff) - without edges. As the
result what could be emitted as "K\x00." was emitted as
"J\x00\x00\x00\x00." and so on.

Fix it by emitting BININT1 for [0, 0xff] inclusive and BININT for [0,
0xffff] also inclusive.
parent db0cbaf8
...@@ -440,10 +440,10 @@ func (e *Encoder) encodeInt(k reflect.Kind, i int64) error { ...@@ -440,10 +440,10 @@ func (e *Encoder) encodeInt(k reflect.Kind, i int64) error {
// protocol >= 1: BININT* // protocol >= 1: BININT*
if e.config.Protocol >= 1 { if e.config.Protocol >= 1 {
switch { switch {
case i > 0 && i < math.MaxUint8: case i >= 0 && i <= math.MaxUint8:
return e.emit(opBinint1, byte(i)) return e.emit(opBinint1, byte(i))
case i > 0 && i < math.MaxUint16: case i >= 0 && i <= math.MaxUint16:
return e.emit(opBinint2, byte(i), byte(i >> 8)) return e.emit(opBinint2, byte(i), byte(i >> 8))
case i >= math.MinInt32 && i <= math.MaxInt32: case i >= math.MinInt32 && i <= math.MaxInt32:
......
...@@ -191,14 +191,26 @@ var tests = []TestEntry{ ...@@ -191,14 +191,26 @@ var tests = []TestEntry{
P01("I00\n."), // INT 00 P01("I00\n."), // INT 00
P2_("\x89.")), // NEWFALSE P2_("\x89.")), // NEWFALSE
X("int(0)", int64(0),
P0("I0\n."), // INT
P1_("K\x00.")), // BININT1
X("int(5)", int64(5), X("int(5)", int64(5),
P0("I5\n."), // INT P0("I5\n."), // INT
P1_("K\x05.")), // BININT1 P1_("K\x05.")), // BININT1
X("int(0xff)", int64(0xff),
P0("I255\n."), // INT
P1_("K\xff.")), // BININT1
X("int(0x123)", int64(0x123), X("int(0x123)", int64(0x123),
P0("I291\n."), // INT P0("I291\n."), // INT
P1_("M\x23\x01.")), // BININT2 P1_("M\x23\x01.")), // BININT2
X("int(0xffff)", int64(0xffff),
P0("I65535\n."), // INT
P1_("M\xff\xff.")), // BININT2
X("int(0x12345)", int64(0x12345), X("int(0x12345)", int64(0x12345),
P0("I74565\n."), // INT P0("I74565\n."), // INT
P1_("J\x45\x23\x01\x00.")), // BININT P1_("J\x45\x23\x01\x00.")), // BININT
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment