Skip to content

Commit cb7fd0b

Browse files
committed
fix IP2Long nil pointer panic bug
1 parent 9c2e114 commit cb7fd0b

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

network.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ func IP2Long(ipAddress string) uint32 {
5555
if ip == nil {
5656
return 0
5757
}
58-
return binary.BigEndian.Uint32(ip.To4())
58+
ipByte := ip.To4()
59+
if ipByte == nil {
60+
return 0
61+
}
62+
63+
return binary.BigEndian.Uint32(ipByte)
5964
}
6065

6166
// Long2IP converts an long integer address into a string in (IPv4) Internet standard dotted format

network_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var _ = Describe("Network Functions", func() {
3939
It("IP2Long", func() {
4040
Expect(php.IP2Long("127.0.0.1")).To(Equal(uint32(2130706433)))
4141
Expect(php.IP2Long("333.33.3.3")).To(BeZero())
42+
Expect(php.IP2Long("::1")).To(BeZero())
4243
})
4344

4445
It("Long2IP", func() {

0 commit comments

Comments
 (0)