summaryrefslogtreecommitdiff
path: root/sbin/route/show.c
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>1998-09-21 08:31:47 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>1998-09-21 08:31:47 +0000
commited0d228fb006413706e55cdc6d32934a1a20c16b (patch)
tree662b34019a3bfa2037d763d40166cc08653ab94c /sbin/route/show.c
parentb035152d559c05bd0c273db0c645e9a76ab61122 (diff)
bad sysctl(), malloc(0), and memory leaks fixes
Diffstat (limited to 'sbin/route/show.c')
-rw-r--r--sbin/route/show.c33
1 files changed, 19 insertions, 14 deletions
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);
}
}