From 53ca29efadb8b3f4467a0f1e6782b7004e6b23c6 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Thu, 10 Oct 2013 00:20:14 +0100 Subject: [PATCH] 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. --- stack.go | 2 +- stack_test.go | 34 ++++++++++++++++++++++++++++++++++ test_coverage.txt | 4 ++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/stack.go b/stack.go index 2a1cc556..0f72e2a0 100644 --- a/stack.go +++ b/stack.go @@ -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] diff --git a/stack_test.go b/stack_test.go index 19166aaf..e0b09995 100644 --- a/stack_test.go +++ b/stack_test.go @@ -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", diff --git a/test_coverage.txt b/test_coverage.txt index fdcee9cc..cddc5fc8 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -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)