SDL: Windows process: escape backslashes before quotes

From 3cf54675bb0db6173519a05866c48a007199f03d Mon Sep 17 00:00:00 2001
From: Semphris <[EMAIL REDACTED]>
Date: Fri, 13 Sep 2024 22:23:49 -0400
Subject: [PATCH] Windows process: escape backslashes before quotes

---
 src/process/windows/SDL_windowsprocess.c | 10 ++++++++++
 test/testprocess.c                       |  3 +++
 2 files changed, 13 insertions(+)

diff --git a/src/process/windows/SDL_windowsprocess.c b/src/process/windows/SDL_windowsprocess.c
index 2ae13022a2fd9..721e61a0f23c9 100644
--- a/src/process/windows/SDL_windowsprocess.c
+++ b/src/process/windows/SDL_windowsprocess.c
@@ -95,6 +95,10 @@ static bool join_arguments(const char * const *args, char **args_out)
             case '"':
                 len += 2;
                 break;
+            case '\\':
+                /* only escape backslashes that precede a double quote */
+                len += (*(a + 1) == '"' || *(a + 1) == '\0') ? 2 : 1;
+                break;
             default:
                 len += 1;
                 break;
@@ -121,6 +125,12 @@ static bool join_arguments(const char * const *args, char **args_out)
                 result[i_out++] = '\\';
                 result[i_out++] = *a;
                 break;
+            case '\\':
+                result[i_out++] = *a;
+                if (*(a + 1) == '"' || *(a + 1) == '\0') {
+                    result[i_out++] = *a;
+                }
+                break;
             default:
                 result[i_out++] = *a;
                 break;
diff --git a/test/testprocess.c b/test/testprocess.c
index 9898e516ae907..6f59c5ad22800 100644
--- a/test/testprocess.c
+++ b/test/testprocess.c
@@ -92,6 +92,9 @@ static int SDLCALL process_testArguments(void *arg)
         "'a' 'b' 'c'",
         "%d%%%s",
         "\\t\\c",
+        "evil\\",
+        "a\\b\"c\\",
+        "\"\\^&|<>%", /* characters with a special meaning */
         NULL
     };
     SDL_Process *process = NULL;