2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-23 14:57:06 +00:00

packets: Make ip_parse_masked() pickier about formatting.

It's happened a couple of times now that I've entered a typoed IP address,
e.g. "192.168.0.0$x", and ip_parse_masked() or its predecessor has accepted
it anyway, and it's been hard to track down the real problem.  This change
makes the parser pickier, by disallowing trailing garbage.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Justin Pettit <jpettit@nicira.com>
This commit is contained in:
Ben Pfaff
2015-10-17 14:03:53 -07:00
parent 9975d7beb3
commit a6755d50e5

View File

@@ -415,17 +415,19 @@ char * OVS_WARN_UNUSED_RESULT
ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
{
int prefix;
int n;
if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT,
IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask))) {
if (ovs_scan(s, IP_SCAN_FMT"/"IP_SCAN_FMT"%n",
IP_SCAN_ARGS(ip), IP_SCAN_ARGS(mask), &n) && !s[n]) {
/* OK. */
} else if (ovs_scan(s, IP_SCAN_FMT"/%d", IP_SCAN_ARGS(ip), &prefix)) {
} else if (ovs_scan(s, IP_SCAN_FMT"/%d%n", IP_SCAN_ARGS(ip), &prefix, &n)
&& !s[n]) {
if (prefix <= 0 || prefix > 32) {
return xasprintf("%s: network prefix bits not between 0 and "
"32", s);
}
*mask = be32_prefix_mask(prefix);
} else if (ovs_scan(s, IP_SCAN_FMT, IP_SCAN_ARGS(ip))) {
} else if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(ip), &n) && !s[n]) {
*mask = OVS_BE32_MAX;
} else {
return xasprintf("%s: invalid IP address", s);