1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-27 18:22:24 +00:00
putty-source/windows/utils/getdlgitemtext_alloc.c
Simon Tatham e7acb9f696 GetDlgItemTextW_alloc: use the right memchr.
When retrieving Unicode text from an edit box in the GUI configurer,
we were using plain memchr() to look for a terminating NUL. But of
course you have to use wmemchr() to look for a UTF-16 NUL, or else
memchr() will generate a false positive on the UTF-16 version of (at
least) any ASCII character!

(I also have to provide a fallback implementation of wmemchr for the
w32old builds, which don't have it in the libc they build against.
It's as simple as possible, and we use the libc version where
possible.)
2025-01-13 21:12:40 +00:00

36 lines
745 B
C

/*
* Handy wrappers around GetDlgItemText (A and W) which don't make you
* invent an arbitrary length limit on the output string. Returned
* string is dynamically allocated; caller must free.
*/
#include <wchar.h>
#include "putty.h"
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;
}
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;
}