[ovs-dev] [PATCH] packets: Make ip_parse_masked() pickier about formatting.

Ben Pfaff blp at nicira.com
Sat Oct 17 21:03:53 UTC 2015


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 at nicira.com>
---
 lib/packets.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/packets.c b/lib/packets.c
index e7d0cb3..342d8b7 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -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);
-- 
2.1.3




More information about the dev mailing list