From 22ca3704d2def6eaea1249007c3897d86022a43f Mon Sep 17 00:00:00 2001 From: jfreegman Date: Wed, 16 Mar 2022 16:31:41 -0400 Subject: [PATCH] Use a small hack to get around an ncurses buffer overread Patch by iphydf --- src/line_info.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/line_info.c b/src/line_info.c index c622374..652117b 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -143,11 +143,14 @@ static struct line_info *line_info_ret_queue(struct history *hst) */ static int print_n_chars(WINDOW *win, const wchar_t *s, size_t n, int max_y) { + // we use an array to represent a single wchar in order to get around an ncurses + // bug with waddnwstr() that overreads the memory address by one byte when + // supplied with a single wchar. + wchar_t ch[2] = {0}; bool newline = false; - wchar_t ch; - for (size_t i = 0; i < n && (ch = s[i]); ++i) { - if (ch == L'\n') { + for (size_t i = 0; i < n && (ch[0] = s[i]); ++i) { + if (ch[0] == L'\n') { newline = true; int x; @@ -164,11 +167,11 @@ static int print_n_chars(WINDOW *win, const wchar_t *s, size_t n, int max_y) if (win) { #ifdef HAVE_WIDECHAR - waddnwstr(win, &ch, 1); + waddnwstr(win, ch, 1); #else char b; - if (wcstombs(&b, &ch, sizeof(char)) != 1) { + if (wcstombs(&b, ch, sizeof(char)) != 1) { continue; }