Skip to content

json unmarshaller accepts (and overflows) oversized decimal input #184

@guidovranken

Description

@guidovranken
package main

import (
    "github.com/holiman/uint256"
    "encoding/json"
    "fmt"
)

func main() {
    var v1, v2 uint256.Int

    // As hex
    if err := json.Unmarshal([]byte("\"0x10000000000000000000000000000000000000000000000000000000000000001\""), &v1); err != nil {
        fmt.Println(err)
    }

    // As decimal
    if err := json.Unmarshal([]byte("115792089237316195423570985008687907853269984665640564039457584007913129639937"), &v2); err != nil {
        return
    }
    fmt.Println("As string:", v2.String()) // Prints 1
}

The decimal number being deserialized here is 2**256+1, which exceeds the width of uint256.Int. Ideally json.Unmarshal should return an error for any oversized input but instead it appears to overflow the number.

The same value as a hex value is correctly rejected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions