Skip to content

Commit

Permalink
fix: panic when the ip network has no mask
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jan 21, 2019
1 parent 9d64b16 commit df8cbe4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions ipnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ func NewIPNetwork(ipnet string) (IPNetwork, error) {
var mask int
index := strings.IndexByte(ipnet, '/')
if index == -1 {
index = len(ipnet)
if strings.IndexByte(ipnet, ':') == -1 {
mask = 32
} else {
Expand Down
13 changes: 13 additions & 0 deletions ipnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ package netaddr

import (
"fmt"
"testing"
)

func TestNewIPNetwork(t *testing.T) {
net := MustNewIPNetwork("192.168.10.10/24")
if net.String() != "192.168.10.10/24" {
t.Error(net.String())
}

net = MustNewIPNetwork("192.168.10.10")
if net.String() != "192.168.10.10/32" {
t.Error(net.String())
}
}

func ExampleIPNetwork_Network() {
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.Network())
Expand Down

0 comments on commit df8cbe4

Please sign in to comment.