| Ticket UUID: | 6dacb3b6f0381d534f37ff419ab3b1a9028d04bc | ||
| Title: | exec and open | fail for .bat files if 8dot3name is disabled and path + command contain spaces | ||
| Type: | Bug | Created on: | 2013-07-22 14:01:12 |
| Submitter: | anonymous | Assigned to: | nobody |
| Subsystem: | 52. Portability Support | Severity: | Critical |
| Priority: | 5 Medium | Last modified: | 2026-11-14 18:26:36 |
| Status: | Closed | Closed by: | oehhar |
| Resolution: | Fixed | Closed on: | 2026-11-14 18:26:36 |
| Version: | 8.6.18, 9.0.5, 9.1.0 | ||
| Description: | ||||
The issue is that quoting in tclWinPipe.c does not properly handle cases where a bat is run from GUI (which adds cmd.exe /c to run command) and arguments include quotes as well.
To reproduce:
- disable 8dot3name (fsutil 8dot3name set 1 from elevated command prompt) and reboot
- create a dir with spaces - i.e. C:\dir with spaces
- create C:\dir with spaces\test.bat
- exec C:/dir with spaces/test.bat ""
Tcl fails with error:
'C:/dir' is not recognized as an internal or external command.
This is because internally the following command line is built:
C:\Windows\system32\cmd.exe /c "C:\dir with spaces\test.bat" ""
In this case (where more than 2 quotes are used in cmd /c invocation), cmd.exe strips first and last quotation mark and in turn tries to run:
C:\dir with spaces\test.bat" "
The proper solution is to add /s switch (which will always cause first and last quote to be stripped, regardless of number of quotes) and wrap the whole command line in quotes:
C:\Windows\system32\cmd.exe /c ""C:\dir with spaces\test.bat" """
Which will in turn run:
"C:\dir with spaces\test.bat" ""
More information and quote from cmd.exe /?:
http://stackoverflow.com/questions/355988/how-do-i-deal-with-quote-characters-when-using-cmd-exe
Sample patch to fix the behavior:
--- tclWinPipe.c.orig 2013-07-10 12:56:21 +0000
+++ tclWinPipe.c 2013-07-22 13:51:14 +0000
@@ -1126,7 +1126,7 @@
startInfo.wShowWindow = SW_HIDE;
startInfo.dwFlags |= STARTF_USESHOWWINDOW;
createFlags = CREATE_NEW_CONSOLE;
- Tcl_DStringAppend(&cmdLine, "cmd.exe /c", -1);
+ Tcl_DStringAppend(&cmdLine, "cmd.exe /s /c", -1);
} else {
createFlags = DETACHED_PROCESS;
}
@@ -1539,6 +1539,7 @@
{
const char *arg, *start, *special;
int quote, i;
+ int entireCommandQuote = 0;
Tcl_DString ds;
Tcl_DStringInit(&ds);
@@ -1549,7 +1550,15 @@
Tcl_DStringAppend(&ds, Tcl_DStringValue(linePtr), -1);
if (Tcl_DStringLength(linePtr) > 0) {
- Tcl_DStringAppend(&ds, " ", 1);
+ // On Windows NT, cmd /c /s requires quoting all parameters
+ // with a quote character in order to preserve inner quotes
+ // of the command to be run
+ if (TclWinGetPlatformId() == VER_PLATFORM_WIN32_NT) {
+ Tcl_DStringAppend(&ds, " \"", 2);
+ entireCommandQuote = 1;
+ } else {
+ Tcl_DStringAppend(&ds, " ", 1);
+ }
}
for (i = 0; i < argc; i++) {
@@ -1617,6 +1626,9 @@
Tcl_DStringAppend(&ds, "\"", 1);
}
}
+ if (entireCommandQuote) {
+ Tcl_DStringAppend(&ds, "\"", 1);
+ }
Tcl_DStringFree(linePtr);
Tcl_WinUtfToTChar(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds), linePtr);
Tcl_DStringFree(&ds);
| ||||
| User Comments: | ||||
serhiy.storchaka added on 2026-10-14 15:33:39:
Still reproducible with wish 8.6.18, 9.0.5 and 9.1, for a batch file in a directory with spaces and any argument that needs quoting; tclsh works. It also happens with 8dot3 names enabled. Proposed fix in branch bug-f91ab723f3-quoting (against 8.6), see [f91ab723f3]: the command after serhiy.storchaka added on 2026-10-25 08:54:48:
The fix is rebased onto main as bug-f91ab723f3-quoting-main, see [f91ab723f3]. oehhar added on 2026-11-14 18:26:36:
Fixed by: [f91ab723] Closing. And by the way: Wojciech, I love your book ! Thanks, Harald | ||||
