diff options
author | Tom Cosgrove <tom@cvs.openbsd.org> | 2017-09-01 07:31:46 +0000 |
---|---|---|
committer | Tom Cosgrove <tom@cvs.openbsd.org> | 2017-09-01 07:31:46 +0000 |
commit | 9acbb37aeaf66fdc1b892fab8d2636cecbecbb15 (patch) | |
tree | 02238fb1221d715035790b2a034adb00a9f7a93e | |
parent | f30690be73c343a81af6333348c77857a0f02dec (diff) |
free() the memory we allocate in dmesg.c. While this memory would be
released on process exit, otto@ points out that various checks happen
on free(), so this helps detect memory management errors.
Based on a diff from Nan Xiao - thanks.
ok otto@
-rw-r--r-- | sbin/dmesg/dmesg.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/sbin/dmesg/dmesg.c b/sbin/dmesg/dmesg.c index e11dd6a7f50..35ff84e5886 100644 --- a/sbin/dmesg/dmesg.c +++ b/sbin/dmesg/dmesg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dmesg.c,v 1.28 2017/08/26 08:53:20 tom Exp $ */ +/* $OpenBSD: dmesg.c,v 1.29 2017/09/01 07:31:45 tom Exp $ */ /* $NetBSD: dmesg.c,v 1.8 1995/03/18 14:54:49 cgd Exp $ */ /*- @@ -66,6 +66,7 @@ main(int argc, char *argv[]) char *p; struct msgbuf cur; char *memf, *nlistf, *bufdata = NULL; + char *allocated = NULL; int startupmsgs = 0; char buf[5]; @@ -100,7 +101,7 @@ main(int argc, char *argv[]) "KERN_MSGBUFSIZE"); msgbufsize += sizeof(struct msgbuf) - 1; - bufdata = calloc(1, msgbufsize); + allocated = bufdata = calloc(1, msgbufsize); if (bufdata == NULL) errx(1, "couldn't allocate space for buffer data"); @@ -141,7 +142,7 @@ main(int argc, char *argv[]) (unsigned long)bufp); if (cur.msg_magic != MSG_MAGIC) errx(1, "magic number incorrect"); - bufdata = malloc(cur.msg_bufs); + allocated = bufdata = malloc(cur.msg_bufs); if (bufdata == NULL) errx(1, "couldn't allocate space for buffer data"); if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata, @@ -183,6 +184,7 @@ main(int argc, char *argv[]) } if (!newl) putchar('\n'); + free(allocated); return (0); } |