CMD inconsistent parsing behavior when the /C argument is present #13042

Open
opened 2026-01-31 03:32:08 +00:00 by claunia · 0 comments
Owner

Originally created by @snake-4 on GitHub (Mar 15, 2021).

Environment

Windows build number: Microsoft Windows [Version 10.0.19042.867]
Windows Terminal version (if applicable): N/A

Any other software? A simple test app written in C.

Steps to reproduce

Compile the following C program with any compiler of your choice.

#include <stdio.h>
#include <Windows.h>

int main(int argc, char* argv[])
{
	HANDLE hComPort = CreateFileW(L"\\\\.\\COM9", GENERIC_READ | GENERIC_WRITE, 0, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	printf("Can open: %s\nGetLastError(): %x", (hComPort == INVALID_HANDLE_VALUE ? "NO" : "YES"), GetLastError());
}

Now run the program like this cmd /C "testprog COM9" and it won't be able to open the port and the last error will be 5.
But if you run the program without any arguments, it will be able to open the port just fine. I think what's happening here is that the cmd is somehow opening the port itself.

Expected behavior

Running the program mentioned above with a command like this cmd /C "testprog COM9" should be able to open the port.

Actual behavior

There are two possible outcomes for every test here, either it fails and the last error gets set to 5 or it works fine.
Note: I've performed the following test cases multiple times and tried to restart my computer and each time the outcome was the same.

cmd /C "testprog dummyarg1" works
cmd /C "testprog dummyarg1 COM9" works
cmd /C "testprog dummyarg1 "COM9"" fails
cmd /C "testprog -dummyarg1 COM9" fails

cmd /C "testprog COM9" fails
testprog COM9 works

cmd /e off /C "testprog COM9" fails (*)
cmd /e off /e off /C "testprog COM9" works (*)
cmd /v off /C "testprog COM9" fails (*)
cmd /v off /v off /C "testprog COM9" works (*)

(*) These starred test cases are the most interesting ones because I think they clearly indicate a problem with the cmd's parsing.

Originally created by @snake-4 on GitHub (Mar 15, 2021). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment ```none Windows build number: Microsoft Windows [Version 10.0.19042.867] Windows Terminal version (if applicable): N/A Any other software? A simple test app written in C. ``` # Steps to reproduce Compile the following C program with any compiler of your choice. ```C #include <stdio.h> #include <Windows.h> int main(int argc, char* argv[]) { HANDLE hComPort = CreateFileW(L"\\\\.\\COM9", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); printf("Can open: %s\nGetLastError(): %x", (hComPort == INVALID_HANDLE_VALUE ? "NO" : "YES"), GetLastError()); } ``` Now run the program like this `cmd /C "testprog COM9"` and it won't be able to open the port and the last error will be `5`. But if you run the program without any arguments, it will be able to open the port just fine. I think what's happening here is that the cmd is somehow opening the port itself. <!-- A description of how to trigger this bug. --> # Expected behavior Running the program mentioned above with a command like this `cmd /C "testprog COM9"` should be able to open the port. <!-- A description of what you're expecting, possibly containing screenshots or reference material. --> # Actual behavior There are two possible outcomes for every test here, either it fails and the last error gets set to `5` or it works fine. Note: I've performed the following test cases multiple times and tried to restart my computer and each time the outcome was the same. `cmd /C "testprog dummyarg1"` works `cmd /C "testprog dummyarg1 COM9"` works `cmd /C "testprog dummyarg1 "COM9""` fails `cmd /C "testprog -dummyarg1 COM9"` fails `cmd /C "testprog COM9"` fails `testprog COM9` works `cmd /e off /C "testprog COM9"` fails (\*) `cmd /e off /e off /C "testprog COM9"` works (\*) `cmd /v off /C "testprog COM9"` fails (\*) `cmd /v off /v off /C "testprog COM9"` works (\*) (*) These starred test cases are the most interesting ones because I think they clearly indicate a problem with the cmd's parsing. <!-- What's actually happening? -->
claunia added the Needs-TriageNeeds-Tag-FixNeeds-AttentionProduct-Cmd.exe labels 2026-01-31 03:32:09 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#13042