1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-04-01 19:20:13 -05:00

GetDlgItemText_alloc: avoid realloc loop.

This rewrite, due to SATO Kentaro, uses GetWindowTextLength (which I
hadn't known about) to find the correct size to allocate the buffer
the first time, avoiding the need to keep growing it until a call to
GetDlgItemText doesn't have to truncate the result.
This commit is contained in:
Simon Tatham 2025-01-25 11:09:15 +00:00
parent 520fd3d820
commit 05a13d5cf7

View File

@ -10,26 +10,18 @@
char *GetDlgItemText_alloc(HWND hwnd, int id)
{
char *ret = NULL;
size_t size = 0;
do {
sgrowarray_nm(ret, size, size);
GetDlgItemText(hwnd, id, ret, size);
} while (!memchr(ret, '\0', size-1));
return ret;
HWND item = GetDlgItem(hwnd, id);
size_t size = GetWindowTextLengthA(item) + 1;
char *text = snewn(size, char);
GetWindowTextA(item, text, size);
return text;
}
wchar_t *GetDlgItemTextW_alloc(HWND hwnd, int id)
{
wchar_t *ret = NULL;
size_t size = 0;
do {
sgrowarray_nm(ret, size, size);
GetDlgItemTextW(hwnd, id, ret, size);
} while (!wmemchr(ret, L'\0', size-1));
return ret;
HWND item = GetDlgItem(hwnd, id);
size_t size = GetWindowTextLengthW(item) + 1;
wchar_t *text = snewn(size, wchar_t);
GetWindowTextW(item, text, size);
return text;
}