Skip to content

Negative signed integers as indexed event inputs are not decoded correctly #20266

Closed
@shoenseiwaso

Description

System information

Geth version:

$ geth version
Geth
Version: 1.9.6-stable
Git Commit: bd05968077f27f7eb083404dd8448157996a8788
Architecture: amd64
Protocol Versions: [63]
Network Id: 1
Go Version: go1.13.1
Operating System: darwin

OS & Version: macOS 10.14.6

Expected behaviour

Events which emit negative signed integers with size >64 bits are decoded correctly.

Actual behaviour

They are treated as unsigned integers and not decoded correctly.

Steps to reproduce the behaviour

The problem is here:

case abi.IntTy, abi.UintTy:
num := new(big.Int).SetBytes(topics[0][:])

For integers with size >64 bits, negative values will be encoded by the EVM with a size larger than max(int256). For example, -1 for an int256 (or int192, etc.) is 2^256-1, -2 is 2^256-2, etc.

Non-indexed inputs are decoded correctly via readInteger() in accounts/abi/unpack.go:

default:
// the only case lefts for integer is int256/uint256.
// big.SetBytes can't tell if a number is negative, positive on itself.
// On EVM, if the returned number > max int256, it is negative.
ret := new(big.Int).SetBytes(b)
if typ == UintTy {
return ret
}
if ret.Cmp(maxInt256) > 0 {
ret.Add(maxUint256, big.NewInt(0).Neg(ret))
ret.Add(ret, big.NewInt(1))
ret.Neg(ret)
}
return ret
}

To reproduce:

  1. Deploy the following smart contract:
pragma solidity >=0.0.0;

contract TestIntEvents {
    event Int192Event(int192 x);
    event Int192EventIndexed(int192 indexed x);


    function send192(int192 x) public {
        emit Int192Event(x);
    }

    function send192Indexed(int192 x) public {
        emit Int192EventIndexed(x);
    }
}
  1. Try calling send192(-1) and send192Indexed(-1).
  2. Observe the values returned from both events via go-ethereum:
Int192Event(-1)
// Ok

Int192EventIndexed(115792089237316195423570985008687907853269984665640564039457584007913129639935)
// Not Ok! Expected -1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions