git: 0610ba6cdbf2 - main - flua: fbsd: avoid leaking stdin pipes on error

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Wed, 09 Jul 2025 05:12:53 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=0610ba6cdbf2d8bed2619ae78ef2da5ee9c30b94

commit 0610ba6cdbf2d8bed2619ae78ef2da5ee9c30b94
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2025-07-09 05:12:31 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2025-07-09 05:12:31 +0000

    flua: fbsd: avoid leaking stdin pipes on error
    
    Additionally, there's no way to get to the end without a valid
    stdin_pipe[1] at the moment, so don't check for it.  stdin_pipe[0] is
    closed earlier, as the parent shouldn't need the read-side of the pipe.
    
    While we're here, also free the file actions earlier and on error --
    they're not necessary once posix_spawnp() has returned.
    
    Reviewed by:    bapt
    Differential Revision:  https://reviews.freebsd.org/D50537
---
 libexec/flua/modules/lfbsd.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/libexec/flua/modules/lfbsd.c b/libexec/flua/modules/lfbsd.c
index 30cafcc7309e..6279474f8388 100644
--- a/libexec/flua/modules/lfbsd.c
+++ b/libexec/flua/modules/lfbsd.c
@@ -88,13 +88,22 @@ lua_exec(lua_State *L)
 	argv = luaL_checkarraystrings(L, 1);
 	if (0 != (r = posix_spawnp(&pid, argv[0], &action, NULL,
 		(char*const*)argv, environ))) {
+		close(stdin_pipe[0]);
+		close(stdin_pipe[1]);
+		posix_spawn_file_actions_destroy(&action);
+
 		lua_pushnil(L);
 		lua_pushstring(L, strerror(r));
 		lua_pushinteger(L, r);
 		return (3);
 	}
+
+	close(stdin_pipe[0]);
+	posix_spawn_file_actions_destroy(&action);
+
 	while (waitpid(pid, &pstat, 0) == -1) {
 		if (errno != EINTR) {
+			close(stdin_pipe[1]);
 			lua_pushnil(L);
 			lua_pushstring(L, strerror(r));
 			lua_pushinteger(L, r);
@@ -109,12 +118,7 @@ lua_exec(lua_State *L)
 		return (3);
 	}
 
-	posix_spawn_file_actions_destroy(&action);
-
-	if (stdin_pipe[0] != -1)
-		close(stdin_pipe[0]);
-	if (stdin_pipe[1] != -1)
-		close(stdin_pipe[1]);
+	close(stdin_pipe[1]);
 	lua_pushinteger(L, pid);
 	return 1;
 }