Repeated use of OSC4 causes output corruption #15094

Open
opened 2026-01-31 04:28:25 +00:00 by claunia · 0 comments
Owner

Originally created by @vefatica on GitHub (Sep 5, 2021).

Windows Terminal version (or Windows build number)

Microsoft Windows 10 Pro for Workstations 10.0.19043.1165 (2009, 21H1)

Other Software

Batch files in TCC.EXE

Steps to reproduce

I wrote an app to reset the 16-color console palette reproduce any of WT's nine "schemes". It constructs the OSC4 sequence without ESCs, outputs it (error checking), and issues it with the ESCs added. At it's heart is this (full CPP attached).

	for ( INT i=0; i<16; i++ ) // Does conhost have colors 16 and 17?
	{
		// they're backwards
		r = GetBValue(cr[index][i]);
		g = GetGValue(cr[index][i]);
		b = GetRValue(cr[index][i]);
		wsprintfW(seq, L"]4;%d;rgb:%02X/%02X/%02X", i, r, g, b);
		WriteConsoleW(hOut, seq, lstrlen(seq), &dwWritten, nullptr);
		WriteConsoleW(hOut, L"\r\n", lstrlen(L"\r\n"), &dwWritten, nullptr);
		wsprintfW(seq2, L"%c%s%c", L'\x01b', seq, L'\x01b');
		WriteConsoleW(hOut, seq2, lstrlen(seq2), &dwWritten, nullptr);
	}

The program works, but I see corrupted output. Here's the result when run in the general audience CMD/CONHOST (scheme 8 is "Vintage", my default for consoles). Notice that after OSC4 has been issued once, the subsequent test output is corrupt (missing the leading ']'). Aside from that, the app works.

D:\Projects2019\osc4test\x64\Release> osc4test.exe 8
]4;0;rgb:00/00/00
4;1;rgb:80/00/00
4;2;rgb:00/80/00
4;3;rgb:80/80/00
4;4;rgb:00/00/80
4;5;rgb:80/00/80
4;6;rgb:00/80/80
4;7;rgb:C0/C0/C0
4;8;rgb:80/80/80
4;9;rgb:FF/00/00
4;10;rgb:00/FF/00
4;11;rgb:FF/FF/00
4;12;rgb:00/00/FF
4;13;rgb:FF/00/FF
4;14;rgb:00/FF/FF
4;15;rgb:FF/FF/FF

Exactly the same thing happens if I do it in a batch file with this loop at its heart.

:: envvar x is scheme index; i is color index
:: entries in array a look like RRGGBB (hex)
do i=0 to 15
	set r=%@instr[0,2,%a[%x,%i]]
	set g=%@instr[2,2,%a[%x,%i]]
	set b=%@instr[4,2,%a[%x,%i]]
	set seq=]4;%i;rgb:%r/%g/%b
	echo %seq
	echos ^e%seq^e
enddo

Things get worse if I run the EXE in a current OpenConsole.exe (stable or preview) and CMD. All output after the first line is lost. Likewise for the batch file with TCC.EXE running in OpenConsole.exe.

p:\osc4test\x64\Release> osc4test.exe 8
]4;0;rgb:00/00/00

p:\osc4test\x64\Release>

osc4test.src.zip

Expected Behavior

No corruption.

Actual Behavior

As described above.

Originally created by @vefatica on GitHub (Sep 5, 2021). ### Windows Terminal version (or Windows build number) Microsoft Windows 10 Pro for Workstations 10.0.19043.1165 (2009, 21H1) ### Other Software Batch files in TCC.EXE ### Steps to reproduce I wrote an app to reset the 16-color console palette reproduce any of WT's nine "schemes". It constructs the OSC4 sequence without ESCs, outputs it (error checking), and issues it with the ESCs added. At it's heart is this (full CPP attached). ``` for ( INT i=0; i<16; i++ ) // Does conhost have colors 16 and 17? { // they're backwards r = GetBValue(cr[index][i]); g = GetGValue(cr[index][i]); b = GetRValue(cr[index][i]); wsprintfW(seq, L"]4;%d;rgb:%02X/%02X/%02X", i, r, g, b); WriteConsoleW(hOut, seq, lstrlen(seq), &dwWritten, nullptr); WriteConsoleW(hOut, L"\r\n", lstrlen(L"\r\n"), &dwWritten, nullptr); wsprintfW(seq2, L"%c%s%c", L'\x01b', seq, L'\x01b'); WriteConsoleW(hOut, seq2, lstrlen(seq2), &dwWritten, nullptr); } ``` The program works, but I see corrupted output. Here's the result when run in the general audience CMD/CONHOST (scheme 8 is "Vintage", my default for consoles). Notice that after OSC4 has been issued once, the subsequent test output is corrupt (missing the leading ']'). Aside from that, the app works. ``` D:\Projects2019\osc4test\x64\Release> osc4test.exe 8 ]4;0;rgb:00/00/00 4;1;rgb:80/00/00 4;2;rgb:00/80/00 4;3;rgb:80/80/00 4;4;rgb:00/00/80 4;5;rgb:80/00/80 4;6;rgb:00/80/80 4;7;rgb:C0/C0/C0 4;8;rgb:80/80/80 4;9;rgb:FF/00/00 4;10;rgb:00/FF/00 4;11;rgb:FF/FF/00 4;12;rgb:00/00/FF 4;13;rgb:FF/00/FF 4;14;rgb:00/FF/FF 4;15;rgb:FF/FF/FF ``` Exactly the same thing happens if I do it in a batch file with this loop at its heart. ``` :: envvar x is scheme index; i is color index :: entries in array a look like RRGGBB (hex) do i=0 to 15 set r=%@instr[0,2,%a[%x,%i]] set g=%@instr[2,2,%a[%x,%i]] set b=%@instr[4,2,%a[%x,%i]] set seq=]4;%i;rgb:%r/%g/%b echo %seq echos ^e%seq^e enddo ``` Things get worse if I run the EXE in a current OpenConsole.exe (stable or preview) and CMD. All output after the first line is lost. Likewise for the batch file with TCC.EXE running in OpenConsole.exe. ``` p:\osc4test\x64\Release> osc4test.exe 8 ]4;0;rgb:00/00/00 p:\osc4test\x64\Release> ``` [osc4test.src.zip](https://github.com/microsoft/terminal/files/7112030/osc4test.src.zip) ### Expected Behavior No corruption. ### Actual Behavior As described above.
claunia added the Resolution-Fix-AvailableNeeds-TriageIssue-Docs labels 2026-01-31 04:28:25 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#15094