Skip to content

Commit

Permalink
fix IP2Long nil pointer panic bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperjiang committed May 18, 2020
1 parent 9c2e114 commit cb7fd0b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion network.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func IP2Long(ipAddress string) uint32 {
if ip == nil {
return 0
}
return binary.BigEndian.Uint32(ip.To4())
ipByte := ip.To4()
if ipByte == nil {
return 0
}

return binary.BigEndian.Uint32(ipByte)
}

// Long2IP converts an long integer address into a string in (IPv4) Internet standard dotted format
Expand Down
1 change: 1 addition & 0 deletions network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var _ = Describe("Network Functions", func() {
It("IP2Long", func() {
Expect(php.IP2Long("127.0.0.1")).To(Equal(uint32(2130706433)))
Expect(php.IP2Long("333.33.3.3")).To(BeZero())
Expect(php.IP2Long("::1")).To(BeZero())
})

It("Long2IP", func() {
Expand Down

0 comments on commit cb7fd0b

Please sign in to comment.