[#192] Sanity check negative input for amount, gas-price, gas-limit, and nonce

pull/194/head
Janet Liang 5 years ago
parent 80ecdde710
commit 3bffe6e5df
  1. 30
      cmd/subcommands/transfer.go
  2. 5
      pkg/common/numeric.go

@ -2,14 +2,15 @@ package cmd
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/harmony-one/go-sdk/pkg/address"
"github.com/harmony-one/go-sdk/pkg/common"
c "github.com/harmony-one/go-sdk/pkg/common"
"github.com/harmony-one/go-sdk/pkg/rpc"
"github.com/harmony-one/go-sdk/pkg/sharding"
"github.com/harmony-one/go-sdk/pkg/store"
@ -18,7 +19,6 @@ import (
"github.com/harmony-one/harmony/accounts"
"github.com/harmony-one/harmony/core"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -126,13 +126,19 @@ func handlerForTransaction(txLog *transactionLog) error {
if handlerForError(txLog, err) != nil {
return err
}
amt, err := c.NewDecFromString(amount)
if handlerForError(txLog, err) != nil {
return err
amt, err := common.NewDecFromString(amount)
if err != nil {
amtErr := fmt.Errorf("amount %w", err)
handlerForError(txLog, amtErr)
return amtErr
}
gPrice, err := c.NewDecFromString(gasPrice)
if handlerForError(txLog, err) != nil {
return err
gPrice, err := common.NewDecFromString(gasPrice)
if err != nil {
gasErr := fmt.Errorf("gas-price %w", err)
handlerForError(txLog, gasErr)
return gasErr
}
var gLimit uint64
@ -142,6 +148,11 @@ func handlerForTransaction(txLog *transactionLog) error {
return err
}
} else {
if strings.HasPrefix(gasLimit, "-") {
limitErr := errors.New(fmt.Sprintf("gas-limit can not be negative: %s", gasLimit))
handlerForError(txLog, limitErr)
return limitErr
}
tempLimit, e := strconv.ParseInt(gasLimit, 10, 64)
if handlerForError(txLog, e) != nil {
return e
@ -258,6 +269,9 @@ func opts(ctlr *transaction.Controller) {
func getNonceFromInput(addr, inputNonce string, messenger rpc.T) (uint64, error) {
if inputNonce != "" {
if strings.HasPrefix(inputNonce, "-") {
return 0, errors.New(fmt.Sprintf("nonce can not be negative: %s", inputNonce))
}
nonce, err := strconv.ParseUint(inputNonce, 10, 64)
if err != nil {
return 0, err

@ -1,6 +1,8 @@
package common
import (
"errors"
"fmt"
"math/big"
"regexp"
"strconv"
@ -32,6 +34,9 @@ func Pow(base numeric.Dec, exp int) numeric.Dec {
}
func NewDecFromString(i string) (numeric.Dec, error) {
if strings.HasPrefix(i, "-") {
return numeric.ZeroDec(), errors.New(fmt.Sprintf("can not be negative: %s", i))
}
if pattern.FindString(i) != "" {
tokens := strings.Split(i, "e")
a, _ := numeric.NewDecFromStr(tokens[0])

Loading…
Cancel
Save