Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime error: makeslice: len out of range #11

Open
JianJunVictory opened this issue Apr 2, 2019 · 7 comments
Open

runtime error: makeslice: len out of range #11

JianJunVictory opened this issue Apr 2, 2019 · 7 comments

Comments

@JianJunVictory
Copy link

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
Copy link
Owner

ncw commented Apr 2, 2019

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

@JianJunVictory
Copy link
Author

`
// 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
Copy link
Owner

ncw commented Apr 10, 2019

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

The problem with gmp must be here

gmp/int.go

Lines 684 to 699 in 22a0580

func (z *Int) Bytes() []byte {
b := make([]byte, 1+(z.BitLen()+7)/8)
n := C.size_t(len(b))
C.mpz_export(unsafe.Pointer(&b[0]), &n, 1, 1, 1, 0, &z.i[0])
return b[0:n]
}
// BitLen returns the length of the absolute value of z in bits.
// The bit length of 0 is 0.
func (z *Int) BitLen() int {
z.doinit()
if z.Sign() == 0 {
return 0
}
return int(C.mpz_sizeinbase(&z.i[0], 2))
}

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
Copy link
Author

@ncw

The problem with gmp must be here

yes.

If z is a null pointer, will it happen?

@ncw
Copy link
Owner

ncw commented Apr 12, 2019

@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
Copy link
Author

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
Copy link
Owner

ncw commented Apr 16, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants