ncw / gmp

Go language interface to GMP - GNU Multiprecision Library (golang)
BSD 3-Clause "New" or "Revised" License
115 stars 26 forks source link

runtime error: makeslice: len out of range #11

Open JianJunVictory opened 5 years ago

JianJunVictory commented 5 years ago

What's the reason?

type Amount struct{
      gmp.Int
}

when I use Int.Bytes(),it happens at the first time,and then run code again,it's ok.

ncw commented 5 years ago

Can you post a short example of the problem please? Some thing I can run preferably. Thanks

JianJunVictory commented 5 years ago

` // here fmt.Printf("%#v",amount.Int) gmp.Int{i:gmp._Ctype_mpz_t{gmp._Ctype_struct___0{_mp_alloc:1, _mp_size:1, _mp_d:(*gmp._Ctype_ulong)(0x34b5010)}}, init:true}panic: runtime error: makeslice: len out of range

goroutine 1 [running]: github.com/dappley/go-dappley/vendor/github.com/ncw/gmp.(Int).Bytes(0xc42017eee0, 0x4, 0xc4201d1878, 0x1) /home/jun/goPath/src/github.com/dappley/go-dappley/vendor/github.com/ncw/gmp/int.go:685 +0x77 github.com/dappley/go-dappley/core.(Transaction).GetToHashBytes(0xc4200b0320, 0xc4200d2620, 0xc4201d19e8, 0x4176b8) /home/jun/goPath/src/github.com/dappley/go-dappley/core/transaction.go:208 +0x57a github.com/dappley/go-dappley/core.(*Transaction).Hash(0xc4200b0320, 0xc4201c4420, 0x22, 0xc4200a5da0) /home/jun/goPath/src/github.com/dappley/go-dappley/core/transaction.go:222 +0x49 github.com/dappley/go-dappley/core.NewGenesisBlock(0xc91458, 0x22, 0xc4201d1b90) /home/jun/goPath/src/github.com/dappley/go-dappley/core/genesis.go:31 +0x2b6 github.com/dappley/go-dappley/core.CreateBlockchain(0xc91458, 0x22, 0xe36c40, 0xc4200a07f0, 0xe37180, 0xc4200b0280, 0x6400000, 0xe2e0e0, 0xc4200a0810, 0x6400000, ...) /home/jun/goPath/src/github.com/dappley/go-dappley/core/blockchain.go:74 +0x50 github.com/dappley/go-dappley/logic.CreateBlockchain(0xc91458, 0x22, 0xe36c40, 0xc4200a07f0, 0xe37180, 0xc4200b0280, 0xc406400000, 0xc4200a0810, 0x6400000, 0xe2b360, ...) /home/jun/goPath/src/github.com/dappley/go-dappley/logic/logic.go:54 +0xc0 main.main() /home/jun/goPath/src/github.com/dappley/go-dappley/dapp/main.go:86 +0x81d exit status 2

`

https://github.com/dappley/go-dappley/tree/sc I change "math/big" to "gmp" in common / amount.go . When I run "go run main.go" at first time ,it happens.

ncw commented 5 years ago

I couldn't make go-dappley compile :-(

The problem with gmp must be here

https://github.com/ncw/gmp/blob/22a058044a92d3096aec89da13b381dbb7606e42/int.go#L684-L699

z.BitLen() must be returning a negative number for the make([]byte...) to panic

Which means C.mpz_sizeinbase must be returning a negative number.

I don't know why it should do that unless the z pointer is invalid somehow.

Any thoughts on how I can replicate this?

JianJunVictory commented 5 years ago

@ncw

The problem with gmp must be here

yes.

If z is a null pointer, will it happen?

ncw commented 5 years ago

@ncw If z is a null pointer, will it happen?

I tried with z null and calling C.mpz_sizeinbase(nil, 2) and they both give SEGV rather than anything else.

So something weird is going on! Is there concurrency in the test program? What version of libgmp do you have?

JianJunVictory commented 5 years ago

What version of libgmp do you have?

v1.0.4

Is there concurrency in the test program?

Is the program here referring to my own program? if so,yes,there is data race. What you mean is that the passed parameter is a null pointer, caused by data race in my program?

ncw commented 5 years ago

What version of libgmp do you have?

v1.0.4

That seems very old, I'm using 6.1.2 from dpkg -l | grep libgmp

Is there concurrency in the test program?

Is the program here referring to my own program?

The program which gave the error.

if so,yes,there is data race. What you mean is that the passed parameter is a null pointer, caused by data race in my program?

If there are concurrency problems then that can cause memory corruptions.

Have you tried building your program with the go build -race flag?