On Sun, Nov 16, 2014 at 06:38:24PM -0500, Ted Unangst wrote:
> Not sure how I ended up reading this file, but all the redundant
> strlen calls make me twitchy.
I agree, but have you considered the other parser.c files with
match_token and same use of strlen(word), too?
src/usr.sbin/dvmrpctl/parser.c
src/usr.sbin/ikectl/parser.c
src/usr.sbin/iscsictl/parser.c
src/usr.sbin/ldpctl/parser.c
src/usr.sbin/npppctl/parser.c
src/usr.sbin/ospf6ctl/parser.c
src/usr.sbin/ospfctl/parser.c
src/usr.sbin/relayctl/parser.c
src/usr.sbin/ripctl/parser.c
src/usr.sbin/snmpctl/parser.c
> @@ -744,13 +746,14 @@ parse_prefix(const char *word, struct bg
> bzero(addr, sizeof(struct bgpd_addr));
>
> if ((p = strrchr(word, '/')) != NULL) {
> + size_t plen = strlen(p);
> mask = strtonum(p + 1, 0, 128, &errstr);
> if (errstr)
> errx(1, "netmask %s", errstr);
>
> - if ((ps = malloc(strlen(word) - strlen(p) + 1)) == NULL)
> + if ((ps = malloc(wordlen - plen + 1)) == NULL)
> err(1, "parse_prefix: malloc");
> - strlcpy(ps, word, strlen(word) - strlen(p) + 1);
> + strlcpy(ps, word, wordlen - plen + 1);
>
> if (parse_addr(ps, addr) == 0) {
> free(ps);
While at it, the argument of malloc and strlcpy could be synced by
using a variable pslen. It's easier to review that way:
Index: parser.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpctl/parser.c,v
retrieving revision 1.68
diff -u -p -r1.68 parser.c
--- parser.c 5 Jan 2014 20:53:56 -0000 1.68
+++ parser.c 17 Nov 2014 10:20:10 -0000
@@ -744,13 +746,14 @@ parse_prefix(const char *word, struct bg
bzero(addr, sizeof(struct bgpd_addr));
if ((p = strrchr(word, '/')) != NULL) {
+ size_t pslen = wordlen - strlen(p) + 1;
mask = strtonum(p + 1, 0, 128, &errstr);
if (errstr)
errx(1, "netmask %s", errstr);
- if ((ps = malloc(strlen(word) - strlen(p) + 1)) == NULL)
+ if ((ps = malloc(pslen)) == NULL)
err(1, "parse_prefix: malloc");
- strlcpy(ps, word, strlen(word) - strlen(p) + 1);
+ strlcpy(ps, word, pslen);
if (parse_addr(ps, addr) == 0) {
free(ps);