From 51706dbfb20a5e7044a96e97a4e3e506d9e47f63 Mon Sep 17 00:00:00 2001 From: Liu Date: Sat, 14 Nov 2026 00:31:05 +0800 Subject: [PATCH] fix(widgets): fix password mask buffer overflow - allocate room for the terminating wide character when masking text blocks - add a regression that enables password masking before entering text --- src/widgets/textinput.c | 2 +- tests/integration/test_textinput.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/widgets/textinput.c b/src/widgets/textinput.c index 3323412a..6cf302e4 100644 --- a/src/widgets/textinput.c +++ b/src/widgets/textinput.c @@ -296,7 +296,7 @@ static void textinput_process_textblock(ui_widget_t *widget, break; } if (edit->password_char && txtblk->owner == TEXTBLOCK_OWNER_SOURCE) { - wchar_t *text = malloc(sizeof(wchar_t) * txtblk->length + 1); + wchar_t *text = malloc(sizeof(wchar_t) * (txtblk->length + 1)); wcsncpy(text, txtblk->text, txtblk->length + 1); fillchar(text, edit->password_char); layer = edit->layer_mask; diff --git a/tests/integration/test_textinput.c b/tests/integration/test_textinput.c index 09e67636..165050f7 100644 --- a/tests/integration/test_textinput.c +++ b/tests/integration/test_textinput.c @@ -12,6 +12,34 @@ #include #include +static void check_password_input(void) +{ + ui_widget_t *w = ui_create_widget("textinput"); + wchar_t wcs[64]; + + ui_textinput_set_password_char(w, L'*'); + ui_textinput_set_text_w(w, L"secret"); + ui_widget_update(w); + + ctest_equal_bool("password input should keep the text length", + ui_textinput_get_text_length(w) == 6, true); + ctest_equal_bool("password input should preserve the underlying text", + ui_textinput_get_text_w(w, 0, 64, wcs) == 6 && + wcscmp(wcs, L"secret") == 0, + true); + + ui_textinput_set_password_char(w, 0); + ui_widget_update(w); + ctest_equal_bool( + "disabling password masking should restore the plain text", + ui_textinput_get_text_w(w, 0, 64, wcs) == 6 && + wcscmp(wcs, L"secret") == 0, + true); + + ui_widget_destroy(w); + ui_clear_trash(); +} + static void check_multiline_attr(void) { ui_widget_t *single = ui_create_widget("textinput"); @@ -100,6 +128,7 @@ void test_textinput(void) ctest_equal_bool("should retrieve identical inserted wide string", wcscmp(L"hello, world!", wcs) == 0, true); ui_widget_destroy(w); + check_password_input(); check_multiline_attr(); lcui_destroy(); }