packets: Make ip_parse_masked() pickier about formatting.
authorBen Pfaff <blp@nicira.com>
Sat, 17 Oct 2015 21:03:53 +0000 (14:03 -0700)
committerBen Pfaff <blp@nicira.com>
Sat, 17 Oct 2015 21:45:17 +0000 (14:45 -0700)
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>
lib/packets.c

index e7d0cb3..342d8b7 100644 (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);