Date: Tue, 14 Aug 2018 19:12:45 +0000 (UTC) From: David Bright <dab@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r337812 - in head: sys/dev/netmap tests/sys/kqueue/libkqueue Message-ID: <201808141912.w7EJCjB5074948@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: dab Date: Tue Aug 14 19:12:45 2018 New Revision: 337812 URL: https://svnweb.freebsd.org/changeset/base/337812 Log: Fix several memory leaks. The libkqueue tests have several places that leak memory by using an idiom like: puts(kevent_to_str(kevp)); Rework to save the pointer returned from kevent_to_str() and then free() it after it has been used. Reported by: asomers (pointer to Coverity), Coverity CID: 1296063, 1296064, 1296065, 1296066, 1296067, 1350287, 1394960 Sponsored by: Dell EMC Modified: head/sys/dev/netmap/netmap_freebsd.c head/tests/sys/kqueue/libkqueue/common.h head/tests/sys/kqueue/libkqueue/main.c head/tests/sys/kqueue/libkqueue/proc.c Modified: head/sys/dev/netmap/netmap_freebsd.c ============================================================================== --- head/sys/dev/netmap/netmap_freebsd.c Tue Aug 14 18:58:16 2018 (r337811) +++ head/sys/dev/netmap/netmap_freebsd.c Tue Aug 14 19:12:45 2018 (r337812) @@ -1498,7 +1498,7 @@ netmap_kqfilter(struct cdev *dev, struct knote *kn) kn->kn_fop = (ev == EVFILT_WRITE) ? &netmap_wfiltops : &netmap_rfiltops; kn->kn_hook = priv; - knlist_add(&si->si.si_note, kn, 1); + knlist_add(&si->si.si_note, kn, 0); // XXX unlock(priv) ND("register %p %s td %p priv %p kn %p np_nifp %p kn_fp/fpop %s", na, na->ifp->if_xname, curthread, priv, kn, Modified: head/tests/sys/kqueue/libkqueue/common.h ============================================================================== --- head/tests/sys/kqueue/libkqueue/common.h Tue Aug 14 18:58:16 2018 (r337811) +++ head/tests/sys/kqueue/libkqueue/common.h Tue Aug 14 19:12:45 2018 (r337812) @@ -43,7 +43,7 @@ extern char *cur_test_id; int vnode_fd; -extern const char * kevent_to_str(struct kevent *); +extern char * kevent_to_str(struct kevent *); struct kevent * kevent_get(int); struct kevent * kevent_get_timeout(int, int); Modified: head/tests/sys/kqueue/libkqueue/main.c ============================================================================== --- head/tests/sys/kqueue/libkqueue/main.c Tue Aug 14 18:58:16 2018 (r337811) +++ head/tests/sys/kqueue/libkqueue/main.c Tue Aug 14 19:12:45 2018 (r337812) @@ -41,13 +41,16 @@ test_no_kevents(void) int nfds; struct timespec timeo; struct kevent kev; + char *kev_str; puts("confirming that there are no events pending"); memset(&timeo, 0, sizeof(timeo)); nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo); if (nfds != 0) { puts("\nUnexpected event:"); - puts(kevent_to_str(&kev)); + kev_str = kevent_to_str(&kev); + puts(kev_str); + free(kev_str); errx(1, "%d event(s) pending, but none expected:", nfds); } } @@ -61,12 +64,15 @@ test_no_kevents_quietly(void) int nfds; struct timespec timeo; struct kevent kev; + char *kev_str; memset(&timeo, 0, sizeof(timeo)); nfds = kevent(kqfd, NULL, 0, &kev, 1, &timeo); if (nfds != 0) { puts("\nUnexpected event:"); - puts(kevent_to_str(&kev)); + kev_str = kevent_to_str(&kev); + puts(kev_str); + free(kev_str); errx(1, "%d event(s) pending, but none expected:", nfds); } } @@ -193,18 +199,20 @@ kevent_flags_dump(struct kevent *kev) } /* Copied from ../kevent.c kevent_dump() and improved */ -const char * +char * kevent_to_str(struct kevent *kev) { char buf[512]; + char *flags_str = kevent_flags_dump(kev); + char *fflags_str = kevent_fflags_dump(kev); snprintf(&buf[0], sizeof(buf), "[ident=%ju, filter=%d, %s, %s, data=%jd, udata=%p, " "ext=[%jx %jx %jx %jx]", (uintmax_t) kev->ident, kev->filter, - kevent_flags_dump(kev), - kevent_fflags_dump(kev), + flags_str, + fflags_str, (uintmax_t)kev->data, kev->udata, (uintmax_t)kev->ext[0], @@ -212,6 +220,9 @@ kevent_to_str(struct kevent *kev) (uintmax_t)kev->ext[2], (uintmax_t)kev->ext[3]); + free(flags_str); + free(fflags_str); + return (strdup(buf)); } Modified: head/tests/sys/kqueue/libkqueue/proc.c ============================================================================== --- head/tests/sys/kqueue/libkqueue/proc.c Tue Aug 14 18:58:16 2018 (r337811) +++ head/tests/sys/kqueue/libkqueue/proc.c Tue Aug 14 19:12:45 2018 (r337812) @@ -172,6 +172,7 @@ proc_track(int sleep_time) int gchild_note = 0; pid_t gchild_pid = -1; int done = 0; + char *kev_str; while (!done) { @@ -182,7 +183,9 @@ proc_track(int sleep_time) if (kevp == NULL) { done = 1; } else { - printf(" -- Received kevent: %s\n", kevent_to_str(kevp)); + kev_str = kevent_to_str(kevp); + printf(" -- Received kevent: %s\n", kev_str); + free(kev_str); if ((kevp->fflags & NOTE_CHILD) && (kevp->fflags & NOTE_EXIT)) { errx(1, "NOTE_CHILD and NOTE_EXIT in same kevent: %s", kevent_to_str(kevp));
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808141912.w7EJCjB5074948>