git: 331b84b5db11 - main - rtsold: fix memory leak in script execution

From: Eric van Gyzen <vangyzen_at_FreeBSD.org>
Date: Fri, 25 Feb 2022 15:31:06 UTC
The branch main has been updated by vangyzen:

URL: https://cgit.FreeBSD.org/src/commit/?id=331b84b5db11b17d1bb0cdbce986efb587a1e5ee

commit 331b84b5db11b17d1bb0cdbce986efb587a1e5ee
Author:     Eric van Gyzen <vangyzen@FreeBSD.org>
AuthorDate: 2022-02-23 16:49:12 +0000
Commit:     Eric van Gyzen <vangyzen@FreeBSD.org>
CommitDate: 2022-02-25 15:30:29 +0000

    rtsold: fix memory leak in script execution
    
    Since commit 04e9edb54428, rtsold has leaked the memory for the
    argument vector of every script it runs.
    
    Reported by:    Coverity
    Reviewed by:    markj
    Fixes:          04e9edb5442826a14616157962361ff81e4a38fe
    MFC after:      1 week
    Sponsored by:   Dell EMC Isilon
    Differential Revision:  https://reviews.freebsd.org/D34355
---
 usr.sbin/rtsold/cap_script.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/rtsold/cap_script.c b/usr.sbin/rtsold/cap_script.c
index 5594d8ec8ec5..971d0bae4172 100644
--- a/usr.sbin/rtsold/cap_script.c
+++ b/usr.sbin/rtsold/cap_script.c
@@ -162,7 +162,7 @@ script_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
 	const char *const *iargv, *const *scripts;
 	char **argv;
 	size_t argc, i, nscripts;
-	int fd, status;
+	int error, fd, status;
 
 	if (strcmp(cmd, "script_wait") == 0) {
 		/* Wait for the result of a previous "script_run" command. */
@@ -198,8 +198,10 @@ script_command(const char *cmd, const nvlist_t *limits, nvlist_t *nvlin,
 	memcpy(argv, iargv, sizeof(*argv) * argc);
 
 	fd = script_run(argv);
+	error = errno;
+	free(argv);
 	if (fd < 0)
-		return (errno);
+		return (error);
 
 	(void)caph_rights_limit(fd, cap_rights_init(&rights, CAP_WRITE));
 	nvlist_move_descriptor(nvlout, "fd", fd);