Check file size before reading it into memory

This commit is contained in:
Adam Schmideg 2020-03-03 10:34:52 +01:00
parent 745f0fd521
commit ae3c77b82b

View file

@ -25,6 +25,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"os"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
@ -164,12 +165,17 @@ func HexToECDSA(hexkey string) (*ecdsa.PrivateKey, error) {
// LoadECDSA loads a secp256k1 private key from the given file. // LoadECDSA loads a secp256k1 private key from the given file.
func LoadECDSA(file string) (*ecdsa.PrivateKey, error) { func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
buf, err := ioutil.ReadFile(file) stat, err := os.Stat(file)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(buf) != 64 { size := stat.Size()
return nil, fmt.Errorf("expected 64 hexa characters, got %v", len(buf)) if size != 64 {
return nil, fmt.Errorf("expected 64 hexa characters, got %v", size)
}
buf, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
} }
key, err := hex.DecodeString(string(buf)) key, err := hex.DecodeString(string(buf))