diff options
author | Guy Harris <gharris@sonic.net> | 2023-05-05 19:05:00 -0700 |
---|---|---|
committer | Guy Harris <gharris@sonic.net> | 2023-05-05 19:05:00 -0700 |
commit | ecbab714ecbe6893e6415295e4bf2fcc9d0d94b5 (patch) | |
tree | 158928ddcf14df40cb8609991c6bb1d2bc086717 | |
parent | b76f36a07e2ed6aba822eb45e636da58d8bae06d (diff) | |
download | libpcap-ecbab714ecbe6893e6415295e4bf2fcc9d0d94b5.tar.gz |
pcap_nametoportrange: don't have a special case with sscanf().
Do the parsing ourselves; that avoids, for example, sscanf() mishandling
out-of-range numbers.
-rw-r--r-- | nametoaddr.c | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/nametoaddr.c b/nametoaddr.c index 6f1dafe9..67d31343 100644 --- a/nametoaddr.c +++ b/nametoaddr.c @@ -467,40 +467,33 @@ pcap_nametoport(const char *name, int *port, int *proto) int pcap_nametoportrange(const char *name, int *port1, int *port2, int *proto) { - u_int p1, p2; char *off, *cpy; int save_proto; - if (sscanf(name, "%d-%d", &p1, &p2) != 2) { - if ((cpy = strdup(name)) == NULL) - return 0; + if ((cpy = strdup(name)) == NULL) + return 0; - if ((off = strchr(cpy, '-')) == NULL) { - free(cpy); - return 0; - } + if ((off = strchr(cpy, '-')) == NULL) { + free(cpy); + return 0; + } - *off = '\0'; + *off = '\0'; - if (pcap_nametoport(cpy, port1, proto) == 0) { - free(cpy); - return 0; - } - save_proto = *proto; + if (pcap_nametoport(cpy, port1, proto) == 0) { + free(cpy); + return 0; + } + save_proto = *proto; - if (pcap_nametoport(off + 1, port2, proto) == 0) { - free(cpy); - return 0; - } + if (pcap_nametoport(off + 1, port2, proto) == 0) { free(cpy); + return 0; + } + free(cpy); - if (*proto != save_proto) - *proto = PROTO_UNDEF; - } else { - *port1 = p1; - *port2 = p2; + if (*proto != save_proto) *proto = PROTO_UNDEF; - } return 1; } |