Index: /trunk/src/kmk/w32/winchildren.c
===================================================================
--- /trunk/src/kmk/w32/winchildren.c	(revision 3204)
+++ /trunk/src/kmk/w32/winchildren.c	(revision 3205)
@@ -1438,7 +1438,8 @@
                         case '"':
                             if (fFlags & (MKWCCWCMD_F_CYGWIN_SHELL | MKWCCWCMD_F_MKS_SHELL))
-                                cwcDstExtra += 1;
+                                cwcDstExtra += 1; /* just an extra '"' */
                             else
-                                cwcDstExtra += 1 + cBackslashes;
+                                cwcDstExtra += 1 + cBackslashes; /* extra '\\' for the '"' and for each preceeding slash. */
+                            cBackslashes = 0;
                             break;
 
@@ -1455,4 +1456,5 @@
                 }
 
+                /* If we're quoting the argument and it ends with trailing '\\', it/they must be escaped. */
                 if (   cBackslashes > 0
                     && paArgInfo[i].fQuoteIt
@@ -1507,5 +1509,5 @@
             /* Do the conversion into the end of the output buffer, then move
                it up to where it should be char by char.  */
-            size_t          cBackslashes;
+            int             cBackslashes;
             size_t          cwcLeft     = paArgInfo[i].cwcDst - paArgInfo[i].cwcDstExtra;
             WCHAR volatile *pwchSlowSrc = pwszDst + paArgInfo[i].cwcDstExtra;
@@ -1525,13 +1527,20 @@
                 else if (   (fFlags & (MKWCCWCMD_F_CYGWIN_SHELL | MKWCCWCMD_F_HAVE_SH))
                          ==           (MKWCCWCMD_F_CYGWIN_SHELL | MKWCCWCMD_F_HAVE_SH))
+                {
                     *pwchSlowDst++ = L'"'; /* cygwin: '"' instead of '\\', no escaped slashes. */
+                    cBackslashes = 0;
+                }
                 else
                 {
                     if (!(fFlags & (MKWCCWCMD_F_CYGWIN_SHELL | MKWCCWCMD_F_MKS_SHELL)))
-                        cBackslashes = 1;
-                    while (cBackslashes-- > 0)
+                        cBackslashes += 1; /* one extra escape the '"' and one for each preceeding slash. */
+                    while (cBackslashes > 0)
+                    {
                         *pwchSlowDst++ = L'\\';
+                        cBackslashes--;
+                    }
                 }
                 *pwchSlowDst++ = wcSrc;
+                assert((uintptr_t)pwchSlowDst <= (uintptr_t)pwchSlowSrc);
             }
 
