fix multi-byte bug in previous.

Use the right offset when working out if we should mask off the sign bit.
Add more tests that would have caught this case.
This commit is contained in:
Owain G. Ainsworth 2013-10-10 00:20:14 +01:00
parent 4e608c115f
commit 53ca29efad
3 changed files with 37 additions and 3 deletions

View file

@ -33,7 +33,7 @@ func asInt(v []byte) *big.Int {
intArray := make([]byte, len(v))
for i := range v {
// Mask off the sign bit without changing original array.
if i == 0 && start == 0 && negative {
if i == len(v) -1 && start == 0 && negative {
intArray[len(v)-i -1] = v[i] & 0x7f
} else {
intArray[len(v)-i-1] = v[i]

View file

@ -236,6 +236,22 @@ var stackTests = []stackTest{
nil,
[][]byte{},
},
{
"popInt -1",
[][]byte{{0x81}},
func(stack *btcscript.Stack) error {
v, err := stack.PopInt()
if err != nil {
return err
}
if v.Cmp(big.NewInt(-1)) != 0 {
return errors.New("1 != 1 on popInt")
}
return nil
},
nil,
[][]byte{},
},
{
"popInt -1 leading 0",
[][]byte{{0x01,0x00, 0x00, 0x80}},
@ -253,6 +269,24 @@ var stackTests = []stackTest{
nil,
[][]byte{},
},
// Triggers the multibyte case in asInt
{
"popInt -513",
[][]byte{{0x1, 0x82}},
func(stack *btcscript.Stack) error {
v, err := stack.PopInt()
if err != nil {
return err
}
if v.Cmp(big.NewInt(-513)) != 0 {
fmt.Printf("%v != %v\n", v, big.NewInt(-513))
return errors.New("1 != 1 on popInt")
}
return nil
},
nil,
[][]byte{},
},
// Confirm that the asInt code doesn't modify the base data.
{
"peekint nomodify -1",

View file

@ -3,8 +3,8 @@ github.com/conformal/btcscript/address.go scriptToAddrHashTemplate 100.00% (45
github.com/conformal/btcscript/script.go Script.Step 100.00% (37/37)
github.com/conformal/btcscript/script.go parseScriptTemplate 100.00% (30/30)
github.com/conformal/btcscript/opcode.go parsedOpcode.bytes 100.00% (23/23)
github.com/conformal/btcscript/stack.go asInt 100.00% (21/21)
github.com/conformal/btcscript/script.go NewScript 100.00% (18/18)
github.com/conformal/btcscript/stack.go asInt 100.00% (18/18)
github.com/conformal/btcscript/script.go signatureScriptCustomReader 100.00% (15/15)
github.com/conformal/btcscript/stack.go Stack.nipN 100.00% (14/14)
github.com/conformal/btcscript/stack.go fromInt 100.00% (14/14)
@ -147,5 +147,5 @@ github.com/conformal/btcscript/script.go Script.Execute 44.44% (8/18)
github.com/conformal/btcscript/log.go SetLogWriter 0.00% (0/7)
github.com/conformal/btcscript/script.go IsPushOnlyScript 0.00% (0/4)
github.com/conformal/btcscript/log.go logClosure.String 0.00% (0/1)
github.com/conformal/btcscript --------------------------- 96.40% (936/971)
github.com/conformal/btcscript --------------------------- 96.41% (939/974)