Skip to content

Commit e96bc6a

Browse files
committed
Only allow dotted quad IPv4 addresses
See #40255 This commit makes Class A addresses a parse error in Julia because they are probably not what the user wanted anyway.
1 parent 59f1534 commit e96bc6a

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

stdlib/Sockets/src/IPAddr.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ function parse(::Type{IPv4}, str::AbstractString)
176176
fields = split(str,'.')
177177
i = 1
178178
ret = 0
179+
if length(fields) != 4
180+
throw(ArgumentError("IPv4 addresses must be specified as a dotted quad"))
181+
end
179182
for f in fields
180183
if isempty(f)
181184
throw(ArgumentError("empty field in IPv4 address"))
@@ -185,17 +188,10 @@ function parse(::Type{IPv4}, str::AbstractString)
185188
else
186189
r = parse(Int, f, base = 10)
187190
end
188-
if i != length(fields)
189-
if r < 0 || r > 255
190-
throw(ArgumentError("IPv4 field out of range (must be 0-255)"))
191-
end
192-
ret |= UInt32(r) << ((4-i)*8)
193-
else
194-
if r > ((UInt64(1)<<((5-length(fields))*8))-1)
195-
throw(ArgumentError("IPv4 field too large"))
196-
end
197-
ret |= r
191+
if r < 0 || r > 255
192+
throw(ArgumentError("IPv4 field out of range (must be 0-255)"))
198193
end
194+
ret |= UInt32(r) << ((4-i)*8)
199195
i+=1
200196
end
201197
IPv4(ret)

stdlib/Sockets/test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ sockets_watchdog_timer = Timer(t -> killjob("KILLING BY SOCKETS TEST WATCHDOG\n"
2525

2626
@testset "parsing" begin
2727
@test ip"127.0.0.1" == IPv4(127,0,0,1)
28-
@test ip"192.0" == IPv4(192,0,0,0)
2928

3029
# These used to work, but are now disallowed. Check that they error
3130
@test_throws ArgumentError parse(IPv4, "192.0xFFF") # IPv4(192,0,15,255)
3231
@test_throws ArgumentError parse(IPv4, "192.0xFFFF") # IPv4(192,0,255,255)
3332
@test_throws ArgumentError parse(IPv4, "192.0xFFFFF") # IPv4(192,15,255,255)
3433
@test_throws ArgumentError parse(IPv4, "192.0xFFFFFF") # IPv4(192,255,255,255)
3534
@test_throws ArgumentError parse(IPv4, "022.0.0.1") # IPv4(18,0,0,1)
35+
@test_throws ArgumentError parse(IPv4, "192.0") # IPv4(192,0,0,0)
3636

3737
@test UInt(IPv4(0x01020304)) == 0x01020304
3838
@test Int(IPv4("1.2.3.4")) == Int(0x01020304) == Int32(0x01020304)

0 commit comments

Comments
 (0)