Message ID | 1445115833-16291-1-git-send-email-blp@nicira.com |
---|---|
State | Accepted |
Headers | show |
> On Oct 17, 2015, at 2:03 PM, Ben Pfaff <blp@nicira.com> wrote: > > 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> --Jusitn
On Sat, Oct 17, 2015 at 02:29:40PM -0700, Justin Pettit wrote: > > > On Oct 17, 2015, at 2:03 PM, Ben Pfaff <blp@nicira.com> wrote: > > > > 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> Thanks, applied to master.
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);
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> --- lib/packets.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)