From owner-dev-commits-src-branches@freebsd.org Thu Jun 3 13:40:35 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 21F62639C9E; Thu, 3 Jun 2021 13:40:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Fwn8g07Dnz3KLH; Thu, 3 Jun 2021 13:40:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id A53F62961; Thu, 3 Jun 2021 13:40:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 153DeY2G038564; Thu, 3 Jun 2021 13:40:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 153DeY4f038563; Thu, 3 Jun 2021 13:40:34 GMT (envelope-from git) Date: Thu, 3 Jun 2021 13:40:34 GMT Message-Id: <202106031340.153DeY4f038563@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kristof Provost Subject: git: dec754f05d9c - stable/13 - libpfctl: Improve error handling in pfctl_get_states() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: dec754f05d9cb6ab441dc0993a34e451acbd93dd Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Jun 2021 13:40:35 -0000 The branch stable/13 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=dec754f05d9cb6ab441dc0993a34e451acbd93dd commit dec754f05d9cb6ab441dc0993a34e451acbd93dd Author: Kristof Provost AuthorDate: 2021-05-27 09:43:17 +0000 Commit: Kristof Provost CommitDate: 2021-06-03 13:38:25 +0000 libpfctl: Improve error handling in pfctl_get_states() Ensure that we always free nvlists and other allocated memory. Reviewed by: scottl MFC after: 3 days Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D30493 (cherry picked from commit 27c77f42ae7402c313deec47aa67a8a8e0889410) --- lib/libpfctl/libpfctl.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c index ebc026800a1b..52cd0ed7f36c 100644 --- a/lib/libpfctl/libpfctl.c +++ b/lib/libpfctl/libpfctl.c @@ -731,9 +731,10 @@ int pfctl_get_states(int dev, struct pfctl_states *states) { struct pfioc_nv nv; - nvlist_t *nvl; + nvlist_t *nvl = NULL; const nvlist_t * const *slist; size_t found_count; + int error = 0; bzero(states, sizeof(*states)); TAILQ_INIT(&states->states); @@ -744,14 +745,14 @@ pfctl_get_states(int dev, struct pfctl_states *states) for (;;) { if (ioctl(dev, DIOCGETSTATESNV, &nv)) { - free(nv.data); - return (errno); + error = errno; + goto out; } nvl = nvlist_unpack(nv.data, nv.len, 0); if (nvl == NULL) { - free(nv.data); - return (EIO); + error = EIO; + goto out; } states->count = nvlist_get_number(nvl, "count"); @@ -776,8 +777,10 @@ pfctl_get_states(int dev, struct pfctl_states *states) nv.data = realloc(nv.data, new_size); nv.size = new_size; - if (nv.data == NULL) - return (ENOMEM); + if (nv.data == NULL) { + error = ENOMEM; + goto out; + } continue; } @@ -785,9 +788,8 @@ pfctl_get_states(int dev, struct pfctl_states *states) struct pfctl_state *s = malloc(sizeof(*s)); if (s == NULL) { pfctl_free_states(states); - nvlist_destroy(nvl); - free(nv.data); - return (ENOMEM); + error = ENOMEM; + goto out; } pf_nvstate_to_state(slist[i], s); @@ -796,7 +798,11 @@ pfctl_get_states(int dev, struct pfctl_states *states) break; } - return (0); +out: + nvlist_destroy(nvl); + free(nv.data); + + return (error); } void