SDL: Don't close file descriptors already handled by AddFileDescriptorCloseActions()

From 3b3c4a79b65efb76e1b5f3f10d12061280a59c9c Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 1 Oct 2024 09:14:27 -0700
Subject: [PATCH] Don't close file descriptors already handled by
 AddFileDescriptorCloseActions()

Fixes https://github.com/libsdl-org/SDL/issues/10997
---
 src/process/posix/SDL_posixprocess.c | 36 ----------------------------
 test/testprocess.c                   |  2 +-
 2 files changed, 1 insertion(+), 37 deletions(-)

diff --git a/src/process/posix/SDL_posixprocess.c b/src/process/posix/SDL_posixprocess.c
index ce0d58ce3b7bd..85d6c7c076303 100644
--- a/src/process/posix/SDL_posixprocess.c
+++ b/src/process/posix/SDL_posixprocess.c
@@ -219,27 +219,15 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
             SDL_SetError("posix_spawn_file_actions_adddup2 failed: %s", strerror(errno));
             goto posix_spawn_fail_all;
         }
-        if (posix_spawn_file_actions_addclose(&fa, fd) != 0) {
-            SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-            goto posix_spawn_fail_all;
-        }
         break;
     case SDL_PROCESS_STDIO_APP:
         if (!CreatePipe(stdin_pipe)) {
             goto posix_spawn_fail_all;
         }
-        if (posix_spawn_file_actions_addclose(&fa, stdin_pipe[WRITE_END]) != 0) {
-            SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-            goto posix_spawn_fail_all;
-        }
         if (posix_spawn_file_actions_adddup2(&fa, stdin_pipe[READ_END], STDIN_FILENO) != 0) {
             SDL_SetError("posix_spawn_file_actions_adddup2 failed: %s", strerror(errno));
             goto posix_spawn_fail_all;
         }
-        if (posix_spawn_file_actions_addclose(&fa, stdin_pipe[READ_END]) != 0) {
-            SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-            goto posix_spawn_fail_all;
-        }
         break;
     case SDL_PROCESS_STDIO_NULL:
         if (posix_spawn_file_actions_addopen(&fa, STDIN_FILENO, "/dev/null", O_RDONLY, 0) != 0) {
@@ -261,27 +249,15 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
             SDL_SetError("posix_spawn_file_actions_adddup2 failed: %s", strerror(errno));
             goto posix_spawn_fail_all;
         }
-        if (posix_spawn_file_actions_addclose(&fa, fd) != 0) {
-            SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-            goto posix_spawn_fail_all;
-        }
         break;
     case SDL_PROCESS_STDIO_APP:
         if (!CreatePipe(stdout_pipe)) {
             goto posix_spawn_fail_all;
         }
-        if (posix_spawn_file_actions_addclose(&fa, stdout_pipe[READ_END]) != 0) {
-            SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-            goto posix_spawn_fail_all;
-        }
         if (posix_spawn_file_actions_adddup2(&fa, stdout_pipe[WRITE_END], STDOUT_FILENO) != 0) {
             SDL_SetError("posix_spawn_file_actions_adddup2 failed: %s", strerror(errno));
             goto posix_spawn_fail_all;
         }
-        if (posix_spawn_file_actions_addclose(&fa, stdout_pipe[WRITE_END]) != 0) {
-            SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-            goto posix_spawn_fail_all;
-        }
         break;
     case SDL_PROCESS_STDIO_NULL:
         if (posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, "/dev/null", O_WRONLY, 0644) != 0) {
@@ -309,27 +285,15 @@ bool SDL_SYS_CreateProcessWithProperties(SDL_Process *process, SDL_PropertiesID
                 SDL_SetError("posix_spawn_file_actions_adddup2 failed: %s", strerror(errno));
                 goto posix_spawn_fail_all;
             }
-            if (posix_spawn_file_actions_addclose(&fa, fd) != 0) {
-                SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-                goto posix_spawn_fail_all;
-            }
             break;
         case SDL_PROCESS_STDIO_APP:
             if (!CreatePipe(stderr_pipe)) {
                 goto posix_spawn_fail_all;
             }
-            if (posix_spawn_file_actions_addclose(&fa, stderr_pipe[READ_END]) != 0) {
-                SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-                goto posix_spawn_fail_all;
-            }
             if (posix_spawn_file_actions_adddup2(&fa, stderr_pipe[WRITE_END], STDERR_FILENO) != 0) {
                 SDL_SetError("posix_spawn_file_actions_adddup2 failed: %s", strerror(errno));
                 goto posix_spawn_fail_all;
             }
-            if (posix_spawn_file_actions_addclose(&fa, stderr_pipe[WRITE_END]) != 0) {
-                SDL_SetError("posix_spawn_file_actions_addclose failed: %s", strerror(errno));
-                goto posix_spawn_fail_all;
-            }
             break;
         case SDL_PROCESS_STDIO_NULL:
             if (posix_spawn_file_actions_addopen(&fa, STDERR_FILENO, "/dev/null", O_WRONLY, 0644) != 0) {
diff --git a/test/testprocess.c b/test/testprocess.c
index 97645d0249f1c..5f47010e186e8 100644
--- a/test/testprocess.c
+++ b/test/testprocess.c
@@ -1010,7 +1010,7 @@ static const SDLTest_TestCaseReference processTestBatBadButVulnerability = {
 };
 
 static const SDLTest_TestCaseReference processTestFileRedirection = {
-    process_testFileRedirection, "process_testFileRedirection", "Test redirection from/to files", TEST_DISABLED
+    process_testFileRedirection, "process_testFileRedirection", "Test redirection from/to files", TEST_ENABLED
 };
 
 static const SDLTest_TestCaseReference *processTests[] = {