SetConsoleMode with an invalid mode leaves it tainted #18175

Open
opened 2026-01-31 06:05:56 +00:00 by claunia · 0 comments
Owner

Originally created by @GustavoLCR on GitHub (Aug 14, 2022).

Windows Terminal version

1.15.2002.0

Windows build number

10.0.19044.0

Other Software

No response

Steps to reproduce

#include <stdio.h>
#include <windows.h>

int main() {
	DWORD initial_mode = 0;
	HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
	if (!GetConsoleMode(in, &initial_mode)) {
		puts("GetConsoleMode failed");
		return 1;
	}
	printf("0x%x\n", initial_mode);
	if (!SetConsoleMode(in, -1)) {
		puts("SetConsoleMode failed");
	};
	DWORD new_mode = 0;
	if (!GetConsoleMode(in, &new_mode)) {
		puts("GetConsoleMode failed");
		return 1;
	}
	printf("0x%x\n", new_mode);
	return new_mode != initial_mode;
}

Expected Behavior

Trying to set the console mode to an invalid value should fail and not change it.

Actual Behavior

Setting the console mode with an unsupported flag reports failure but still sets it.
This is unfortunate as it will cause all subsequent calls to SetConsoleMode with a value from GetConsoleMode to report failure.

Originally created by @GustavoLCR on GitHub (Aug 14, 2022). ### Windows Terminal version 1.15.2002.0 ### Windows build number 10.0.19044.0 ### Other Software _No response_ ### Steps to reproduce ```c #include <stdio.h> #include <windows.h> int main() { DWORD initial_mode = 0; HANDLE in = GetStdHandle(STD_INPUT_HANDLE); if (!GetConsoleMode(in, &initial_mode)) { puts("GetConsoleMode failed"); return 1; } printf("0x%x\n", initial_mode); if (!SetConsoleMode(in, -1)) { puts("SetConsoleMode failed"); }; DWORD new_mode = 0; if (!GetConsoleMode(in, &new_mode)) { puts("GetConsoleMode failed"); return 1; } printf("0x%x\n", new_mode); return new_mode != initial_mode; } ``` ### Expected Behavior Trying to set the console mode to an invalid value should fail and not change it. ### Actual Behavior Setting the console mode with an unsupported flag reports failure but still sets it. This is unfortunate as it will cause all subsequent calls to `SetConsoleMode` with a value from `GetConsoleMode` to report failure.
claunia added the Resolution-By-DesignIssue-BugNeeds-Tag-Fix labels 2026-01-31 06:05:56 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#18175