net.ip.Address4: Reject malformed addresses

This commit is contained in:
Eugen Wissner 2018-08-22 06:51:20 +02:00
parent fa4cbb7e59
commit 8ca88d1f01
1 changed files with 14 additions and 1 deletions

View File

@ -290,6 +290,10 @@ if (isInputRange!R && isSomeChar!(ElementType!R))
for (; shift != cond; shift += Address4.step, range.popFront()) for (; shift != cond; shift += Address4.step, range.popFront())
{ {
if (range.empty || range.front == '.')
{
return typeof(return)();
}
result.address |= readIntegral!ubyte(range) << shift; result.address |= readIntegral!ubyte(range) << shift;
if (range.empty || range.front != '.') if (range.empty || range.front != '.')
{ {
@ -297,14 +301,23 @@ if (isInputRange!R && isSomeChar!(ElementType!R))
} }
} }
if (range.empty || range.front == '.')
{
return typeof(return)();
}
result.address |= readIntegral!ubyte(range) << shift; result.address |= readIntegral!ubyte(range) << shift;
return range.empty ? typeof(return)(result) : typeof(return)(); return range.empty ? typeof(return)(result) : typeof(return)();
} }
/// // Rejects malformed addresses
@nogc nothrow pure @safe unittest @nogc nothrow pure @safe unittest
{ {
assert(address4("256.0.0.1").isNothing); assert(address4("256.0.0.1").isNothing);
assert(address4(".0.0.1").isNothing);
assert(address4("0..0.1").isNothing);
assert(address4("0.0.0.").isNothing);
assert(address4("0.0.").isNothing);
assert(address4("").isNothing);
} }
/** /**