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

when serialize Bignum, it drop the negative sign for bignum, would cause error in deserialize #69

Open
atenjin opened this issue Dec 17, 2019 · 1 comment
Assignees
Labels
kind/bug A bug in existing code (including security flaws)

Comments

@atenjin
Copy link

atenjin commented Dec 17, 2019

marshal for bigint is like:

go-ipld-cbor/refmt.go

Lines 27 to 36 in e249008

var BigIntAtlasEntry = atlas.BuildEntry(big.Int{}).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(i big.Int) ([]byte, error) {
return i.Bytes(), nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x []byte) (big.Int, error) {
return *big.NewInt(0).SetBytes(x), nil
})).
Complete()

var BigIntAtlasEntry = atlas.BuildEntry(big.Int{}).Transform().
	TransformMarshal(atlas.MakeMarshalTransformFunc(
		func(i big.Int) ([]byte, error) {
			return i.Bytes(), nil
		})).
	TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
		func(x []byte) (big.Int, error) {
			return *big.NewInt(0).SetBytes(x), nil
		})).
	Complete()

but i.Bytes() would drop the sign for bignum!

// Bytes returns the absolute value of x as a big-endian byte slice.
func (x *Int) Bytes() []byte {
	buf := make([]byte, len(x.abs)*_S)
	return buf[x.abs.bytes(buf):]
}

and when deserialize, the bigint just fill for big.NewInt(0), it's a positive number. So the negative sign is dropped.

I think it not in expectation

@hsanjuan hsanjuan added the kind/bug A bug in existing code (including security flaws) label Mar 6, 2020
@hsanjuan
Copy link
Contributor

hsanjuan commented Mar 6, 2020

@whyrusleeping this seems a like bug... can you have a look or nominate someone familiar to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A bug in existing code (including security flaws)
Projects
None yet
Development

No branches or pull requests

3 participants