Skip to content

Commit 5087222

Browse files
authoredJan 25, 2017
Merge pull request #19811 from JuliaLang/kf/removeipoh
Remove octal/hexadecimal parsing from IPv4. Fixes #9187
2 parents 20347a6 + 90ed5e3 commit 5087222

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed
 

‎base/socket.jl

+7-12
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ end
148148

149149
# Parsing
150150

151+
const ipv4_leading_zero_error = """
152+
Leading zeros in IPv4 addresses are disallowed due to ambiguity.
153+
If the address is in octal or hexadecimal, convert it to decimal, otherwise remove the leading zero.
154+
"""
155+
151156
function parse(::Type{IPv4}, str::AbstractString)
152157
fields = split(str,'.')
153158
i = 1
@@ -156,18 +161,8 @@ function parse(::Type{IPv4}, str::AbstractString)
156161
if isempty(f)
157162
throw(ArgumentError("empty field in IPv4 address"))
158163
end
159-
if f[1] == '0'
160-
if length(f) >= 2 && f[2] == 'x'
161-
if length(f) > 8 # 2+(3*2) - prevent parseint from overflowing on 32bit
162-
throw(ArgumentError("IPv4 field too large"))
163-
end
164-
r = parse(Int,f[3:end],16)
165-
else
166-
if length(f) > 9 # 1+8 - prevent parseint from overflowing on 32bit
167-
throw(ArgumentError("IPv4 field too large"))
168-
end
169-
r = parse(Int,f,8)
170-
end
164+
if length(f) > 1 && f[1] == '0'
165+
throw(ArgumentError(ipv4_leading_zero_error))
171166
else
172167
r = parse(Int,f,10)
173168
end

‎test/socket.jl

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
@test ip"127.0.0.1" == IPv4(127,0,0,1)
44
@test ip"192.0" == IPv4(192,0,0,0)
5-
@test ip"192.0xFFF" == IPv4(192,0,15,255)
6-
@test ip"192.0xFFFF" == IPv4(192,0,255,255)
7-
@test ip"192.0xFFFFF" == IPv4(192,15,255,255)
8-
@test ip"192.0xFFFFFF" == IPv4(192,255,255,255)
9-
@test ip"022.0.0.1" == IPv4(18,0,0,1)
5+
6+
# These used to work, but are now disallowed. Check that they error
7+
@test_throws ArgumentError parse(IPv4, "192.0xFFF") # IPv4(192,0,15,255)
8+
@test_throws ArgumentError parse(IPv4, "192.0xFFFF") # IPv4(192,0,255,255)
9+
@test_throws ArgumentError parse(IPv4, "192.0xFFFFF") # IPv4(192,15,255,255)
10+
@test_throws ArgumentError parse(IPv4, "192.0xFFFFFF") # IPv4(192,255,255,255)
11+
@test_throws ArgumentError parse(IPv4, "022.0.0.1") # IPv4(18,0,0,1)
1012

1113
@test UInt(IPv4(0x01020304)) == 0x01020304
1214
@test Int(IPv4("1.2.3.4")) == Int(0x01020304) == Int32(0x01020304)

0 commit comments

Comments
 (0)