Skip to content

Commit 89097a4

Browse files
authored
Merge pull request #36 from uprendis/feature/ip-flags-err-handling
Add error handling for parsing of IPrestrictFlag/PrivateNodeFlag flags
2 parents 4826d95 + a8619dc commit 89097a4

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

cmd/utils/flags.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import (
3333
"text/template"
3434
"time"
3535

36+
pcsclite "github.com/gballet/go-libpcsclite"
37+
gopsutil "github.com/shirou/gopsutil/mem"
38+
"gopkg.in/urfave/cli.v1"
39+
3640
"github.com/ethereum/go-ethereum/accounts"
3741
"github.com/ethereum/go-ethereum/accounts/keystore"
3842
"github.com/ethereum/go-ethereum/common"
@@ -66,9 +70,6 @@ import (
6670
"github.com/ethereum/go-ethereum/p2p/nat"
6771
"github.com/ethereum/go-ethereum/p2p/netutil"
6872
"github.com/ethereum/go-ethereum/params"
69-
pcsclite "github.com/gballet/go-libpcsclite"
70-
gopsutil "github.com/shirou/gopsutil/mem"
71-
"gopkg.in/urfave/cli.v1"
7273
)
7374

7475
func init() {
@@ -1205,12 +1206,19 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
12051206
cfg.NetRestrict = list
12061207
}
12071208

1209+
var err error
12081210
if iprestrict := ctx.GlobalString(IPrestrictFlag.Name); iprestrict != "" {
1209-
cfg.IPRestrict = netutil.ParseIPs(iprestrict)
1211+
cfg.IPRestrict, err = netutil.ParseIPs(iprestrict)
1212+
if err != nil {
1213+
Fatalf("Option %q: %v", IPrestrictFlag.Name, err)
1214+
}
12101215
}
12111216

12121217
if privatenodes := ctx.GlobalString(PrivateNodeFlag.Name); privatenodes != "" {
1213-
cfg.PrivateNodes = enode.ParseNodes(privatenodes)
1218+
cfg.PrivateNodes, err = enode.ParseNodes(privatenodes)
1219+
if err != nil {
1220+
Fatalf("Option %q: %v", PrivateNodeFlag.Name, err)
1221+
}
12141222
}
12151223

12161224
if ctx.GlobalBool(DeveloperFlag.Name) || ctx.GlobalBool(CatalystFlag.Name) {

p2p/enode/node.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,21 @@ func Parse(validSchemes enr.IdentityScheme, input string) (*Node, error) {
7979
return New(validSchemes, &r)
8080
}
8181

82-
func ParseNodes(s string) []*Node {
82+
func ParseNodes(s string) ([]*Node, error) {
8383
ws := strings.NewReplacer(" ", "", "\n", "", "\t", "")
8484
urls := strings.Split(ws.Replace(s), ",")
8585
nodes := make([]*Node, 0)
8686
for _, url := range urls {
8787
if url == "" {
8888
continue
8989
}
90-
if node, err := Parse(ValidSchemes, url); err == nil {
91-
nodes = append(nodes, node)
90+
node, err := Parse(ValidSchemes, url)
91+
if err != nil {
92+
return nil, fmt.Errorf("couldn't parse enode %v: %v", url, err)
9293
}
94+
nodes = append(nodes, node)
9395
}
94-
return nodes
96+
return nodes, nil
9597
}
9698

9799
// ID returns the node identifier.

p2p/netutil/net.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,21 @@ func ParseNetlist(s string) (*Netlist, error) {
8787
return &l, nil
8888
}
8989

90-
func ParseIPs(s string) []string {
90+
func ParseIPs(s string) ([]string, error) {
9191
ws := strings.NewReplacer(" ", "", "\n", "", "\t", "")
9292
ips := strings.Split(ws.Replace(s), ",")
9393
l := make([]string, 0)
9494
for _, ip := range ips {
9595
if ip == "" {
9696
continue
9797
}
98-
if n := net.ParseIP(ip); n != nil {
99-
l = append(l, n.String())
98+
n := net.ParseIP(ip)
99+
if n == nil {
100+
return nil, fmt.Errorf("couldn't parse IP address %v", ip)
100101
}
102+
l = append(l, n.String())
101103
}
102-
return l
104+
return l, nil
103105
}
104106

105107
// MarshalTOML implements toml.MarshalerRec.

0 commit comments

Comments
 (0)