cmd/evm: evm input minor fixes (#19740)

* cmd/evm: evm input minor fixes, handle prefix, validate length, fixes #18041

* cmd/evm: remove whitespace
This commit is contained in:
Martin Holst Swende 2019-06-25 10:03:04 +02:00 committed by Péter Szilágyi
parent 1da5e0ebb0
commit 2ca89ea479

View File

@ -17,7 +17,6 @@
package main package main
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -121,28 +120,36 @@ func runCmd(ctx *cli.Context) error {
ret []byte ret []byte
err error err error
) )
codeFileFlag := ctx.GlobalString(CodeFileFlag.Name)
codeFlag := ctx.GlobalString(CodeFlag.Name)
// The '--code' or '--codefile' flag overrides code in state // The '--code' or '--codefile' flag overrides code in state
if ctx.GlobalString(CodeFileFlag.Name) != "" { if codeFileFlag != "" || codeFlag != "" {
var hexcode []byte var hexcode []byte
var err error if codeFileFlag != "" {
// If - is specified, it means that code comes from stdin var err error
if ctx.GlobalString(CodeFileFlag.Name) == "-" { // If - is specified, it means that code comes from stdin
//Try reading from stdin if codeFileFlag == "-" {
if hexcode, err = ioutil.ReadAll(os.Stdin); err != nil { //Try reading from stdin
fmt.Printf("Could not load code from stdin: %v\n", err) if hexcode, err = ioutil.ReadAll(os.Stdin); err != nil {
os.Exit(1) fmt.Printf("Could not load code from stdin: %v\n", err)
os.Exit(1)
}
} else {
// Codefile with hex assembly
if hexcode, err = ioutil.ReadFile(codeFileFlag); err != nil {
fmt.Printf("Could not load code from file: %v\n", err)
os.Exit(1)
}
} }
} else { } else {
// Codefile with hex assembly hexcode = []byte(codeFlag)
if hexcode, err = ioutil.ReadFile(ctx.GlobalString(CodeFileFlag.Name)); err != nil {
fmt.Printf("Could not load code from file: %v\n", err)
os.Exit(1)
}
} }
code = common.Hex2Bytes(string(bytes.TrimRight(hexcode, "\n"))) if len(hexcode)%2 != 0 {
fmt.Printf("Invalid input length for hex data (%d)\n", len(hexcode))
} else if ctx.GlobalString(CodeFlag.Name) != "" { os.Exit(1)
code = common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)) }
code = common.FromHex(string(hexcode))
} else if fn := ctx.Args().First(); len(fn) > 0 { } else if fn := ctx.Args().First(); len(fn) > 0 {
// EASM-file to compile // EASM-file to compile
src, err := ioutil.ReadFile(fn) src, err := ioutil.ReadFile(fn)
@ -155,7 +162,6 @@ func runCmd(ctx *cli.Context) error {
} }
code = common.Hex2Bytes(bin) code = common.Hex2Bytes(bin)
} }
initialGas := ctx.GlobalUint64(GasFlag.Name) initialGas := ctx.GlobalUint64(GasFlag.Name)
if genesisConfig.GasLimit != 0 { if genesisConfig.GasLimit != 0 {
initialGas = genesisConfig.GasLimit initialGas = genesisConfig.GasLimit