Some fixes to the type chosen for decoding integers in Go. Fix to ruby encoding at byte boundaries. Add execute bit to ruby tester.

This commit is contained in:
A.J. Beamon 2018-10-31 14:09:23 -07:00
parent 23d5382cea
commit 5035c51d15
3 changed files with 17 additions and 10 deletions

View File

@ -99,6 +99,8 @@ var sizeLimits = []uint64{
1<<(8*8) - 1,
}
var minInt64BigInt = big.NewInt(math.MinInt64)
func bisectLeft(u uint64) int {
var n int
for sizeLimits[n] < u {
@ -188,7 +190,7 @@ func (p *packer) encodeBigInt(i *big.Int) {
panic(fmt.Sprintf("Integer magnitude is too large (more than 255 bytes)"))
}
if i.Sign() > 0 {
if i.Sign() >= 0 {
intBytes := i.Bytes()
if length > 8 {
p.putByte(byte(posIntEnd))
@ -370,7 +372,7 @@ func decodeInt(b []byte) (interface{}, int) {
return uint64(ret), n + 1
}
func decodeBigInt(b []byte) (*big.Int, int) {
func decodeBigInt(b []byte) (interface{}, int) {
val := new(big.Int)
offset := 1
var length int
@ -383,10 +385,8 @@ func decodeBigInt(b []byte) (*big.Int, int) {
offset += 1
} else {
length = int(b[0]) - intZeroCode
if length < 0 {
length = -length
}
// Must be a negative 8 byte integer
length = 8
}
val.SetBytes(b[offset : length+offset])
@ -397,6 +397,11 @@ func decodeBigInt(b []byte) (*big.Int, int) {
val.Sub(val, sub)
}
// This is the only value that fits in an int64 or uint64 that is decoded with this function
if val.Cmp(minInt64BigInt) == 0 {
return val.Int64(), length + offset
}
return val, length + offset
}
@ -448,7 +453,9 @@ func decodeTuple(b []byte, nested bool) (Tuple, int, error) {
el, off = decodeBytes(b[i:])
case b[i] == stringCode:
el, off = decodeString(b[i:])
case negIntStart+1 < b[i] && b[i] < posIntEnd-1:
case negIntStart+1 < b[i] && b[i] < posIntEnd:
el, off = decodeInt(b[i:])
case negIntStart+1 == b[i] && (b[i+1] & 0x80 != 0):
el, off = decodeInt(b[i:])
case negIntStart <= b[i] && b[i] <= posIntEnd:
el, off = decodeBigInt(b[i:])

View File

@ -202,7 +202,7 @@ module FDB
if v == 0
@@INT_ZERO_CODE.chr
elsif v > 0
if v >= @@size_limits[-1]
if v > @@size_limits[-1]
length = (v.bit_length + 7) / 8
result = @@POS_INT_END.chr + length.chr
length.times do |i|
@ -214,8 +214,8 @@ module FDB
(@@INT_ZERO_CODE+n).chr + [v].pack("Q>").slice(8-n, n)
end
else
if -v >= @@size_limits[-1]
length = (v.bit_length + 7) / 8
if -v > @@size_limits[-1]
length = ((-v).bit_length + 7) / 8
v += (1 << (length * 8)) - 1
result = @@NEG_INT_START.chr + (length ^ 0xff).chr
length.times do |i|

0
bindings/ruby/tests/tester.rb Normal file → Executable file
View File