diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1998-09-21 08:31:47 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1998-09-21 08:31:47 +0000 |
commit | ed0d228fb006413706e55cdc6d32934a1a20c16b (patch) | |
tree | 662b34019a3bfa2037d763d40166cc08653ab94c | |
parent | b035152d559c05bd0c273db0c645e9a76ab61122 (diff) |
bad sysctl(), malloc(0), and memory leaks fixes
-rw-r--r-- | sbin/route/route.c | 43 | ||||
-rw-r--r-- | sbin/route/show.c | 33 |
2 files changed, 45 insertions, 31 deletions
diff --git a/sbin/route/route.c b/sbin/route/route.c index ec101635989..80f0eb306c9 100644 --- a/sbin/route/route.c +++ b/sbin/route/route.c @@ -1,4 +1,4 @@ -/* $OpenBSD: route.c,v 1.26 1997/12/12 09:06:08 deraadt Exp $ */ +/* $OpenBSD: route.c,v 1.27 1998/09/21 08:31:46 deraadt Exp $ */ /* $NetBSD: route.c,v 1.16 1996/04/15 18:27:05 cgd Exp $ */ /* @@ -44,7 +44,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)route.c 8.3 (Berkeley) 3/19/94"; #else -static char rcsid[] = "$OpenBSD: route.c,v 1.26 1997/12/12 09:06:08 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: route.c,v 1.27 1998/09/21 08:31:46 deraadt Exp $"; #endif #endif /* not lint */ @@ -230,7 +230,7 @@ flushroutes(argc, argv) { size_t needed; int mib[6], rlen, seqno; - char *buf, *next, *lim; + char *buf = NULL, *next, *lim; register struct rt_msghdr *rtm; if (uid) { @@ -277,16 +277,21 @@ bad: usage(*argv); mib[5] = 0; /* no flags */ if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) quit("route-sysctl-estimate"); - if ((buf = malloc(needed)) == NULL) - quit("malloc"); - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) - quit("actual retrieval of routing table"); - lim = buf + needed; + if (needed) { + if ((buf = malloc(needed)) == NULL) + quit("malloc"); + if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) + quit("actual retrieval of routing table"); + lim = buf + needed; + } if (verbose) { (void) printf("Examining routing table from sysctl\n"); if (af) printf("(address family %s)\n", (*argv + 1)); } + if (buf == NULL) + return; + seqno = 0; /* ??? */ for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; @@ -326,6 +331,7 @@ bad: usage(*argv); (void) printf("done\n"); } } + free(buf); } static char hexlist[] = "0123456789abcdef"; @@ -1074,7 +1080,7 @@ interfaces() { size_t needed; int mib[6]; - char *buf, *lim, *next; + char *buf = NULL, *lim, *next; register struct rt_msghdr *rtm; mib[0] = CTL_NET; @@ -1085,14 +1091,17 @@ interfaces() mib[5] = 0; /* no flags */ if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) quit("route-sysctl-estimate"); - if ((buf = malloc(needed)) == NULL) - quit("malloc"); - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) - quit("actual retrieval of interface table"); - lim = buf + needed; - for (next = buf; next < lim; next += rtm->rtm_msglen) { - rtm = (struct rt_msghdr *)next; - print_rtmsg(rtm, rtm->rtm_msglen); + if (needed) { + if ((buf = malloc(needed)) == NULL) + quit("malloc"); + if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) + quit("actual retrieval of interface table"); + lim = buf + needed; + for (next = buf; next < lim; next += rtm->rtm_msglen) { + rtm = (struct rt_msghdr *)next; + print_rtmsg(rtm, rtm->rtm_msglen); + } + free(buf); } } diff --git a/sbin/route/show.c b/sbin/route/show.c index 527dd1fa5d6..386ded5aa6e 100644 --- a/sbin/route/show.c +++ b/sbin/route/show.c @@ -1,4 +1,4 @@ -/* $OpenBSD: show.c,v 1.6 1998/07/09 01:32:12 deraadt Exp $ */ +/* $OpenBSD: show.c,v 1.7 1998/09/21 08:31:46 deraadt Exp $ */ /* $NetBSD: show.c,v 1.1 1996/11/15 18:01:41 gwr Exp $ */ /* @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94"; #else -static char *rcsid = "$OpenBSD: show.c,v 1.6 1998/07/09 01:32:12 deraadt Exp $"; +static char *rcsid = "$OpenBSD: show.c,v 1.7 1998/09/21 08:31:46 deraadt Exp $"; #endif #endif /* not lint */ @@ -112,7 +112,7 @@ show(argc, argv) char **argv; { register struct rt_msghdr *rtm; - char *buf, *next, *lim; + char *buf = NULL, *next, *lim; size_t needed; int mib[6]; @@ -126,21 +126,26 @@ show(argc, argv) perror("route-sysctl-estimate"); exit(1); } - if ((buf = malloc(needed)) == 0) { - printf("out of space\n"); - exit(1); - } - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { - perror("sysctl of routing table"); - exit(1); + if (needed > 0) { + if ((buf = malloc(needed)) == 0) { + printf("out of space\n"); + exit(1); + } + if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { + perror("sysctl of routing table"); + exit(1); + } + lim = buf + needed; } - lim = buf + needed; printf("Routing tables\n"); - for (next = buf; next < lim; next += rtm->rtm_msglen) { - rtm = (struct rt_msghdr *)next; - p_rtentry(rtm); + if (buf) { + for (next = buf; next < lim; next += rtm->rtm_msglen) { + rtm = (struct rt_msghdr *)next; + p_rtentry(rtm); + } + free(buf); } } |