Skip to content

Commit 855942b

Browse files
authored
fix integer underflow bug in ReadConsoleForTermEmulModern() (#804)
From directvt/vtm#819 (comment) > When a window resize event is received (terminal generates WINDOW_BUFFER_SIZE_EVENT event when switching between alternate/normal buffer mode), the variable text_len=0 may accidentally decrease by one and become text_len=-1 potentially fixes PowerShell/Win32-OpenSSH#2275
1 parent e98b11e commit 855942b

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

contrib/win32/win32compat/tncon.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,13 @@ ReadConsoleForTermEmulModern(HANDLE hInput, char *destin, int destinlen)
183183
}
184184
}
185185

186-
// Pop any lone lead surrogate from the input for later.
187-
const wchar_t last_char = text[text_len - 1];
188-
if (IS_HIGH_SURROGATE(last_char)) {
189-
s_previous_lead = last_char;
190-
text_len--;
186+
if (text_len) {
187+
// Pop any lone lead surrogate from the input for later.
188+
const wchar_t last_char = text[text_len - 1];
189+
if (IS_HIGH_SURROGATE(last_char)) {
190+
s_previous_lead = last_char;
191+
text_len--;
192+
}
191193
}
192194

193195
// ...and finally convert everything to UTF-8.

0 commit comments

Comments
 (0)