1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-04-13 09:08:06 -05:00

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.)
This commit is contained in:
Simon Tatham 2025-01-13 20:43:22 +00:00
parent 293be04298
commit e7acb9f696
6 changed files with 28 additions and 1 deletions

View File

@ -15,6 +15,7 @@
#cmakedefine01 HAVE_GETNAMEDPIPECLIENTPROCESSID
#cmakedefine01 HAVE_SETDEFAULTDLLDIRECTORIES
#cmakedefine01 HAVE_STRTOUMAX
#cmakedefine01 HAVE_WMEMCHR
#cmakedefine01 HAVE_DWMAPI_H
#cmakedefine NOT_X_WINDOWS

View File

@ -53,6 +53,7 @@ define_negation(NO_HTMLHELP HAVE_HTMLHELP_H)
check_include_files("winsock2.h;afunix.h" HAVE_AFUNIX_H)
check_symbol_exists(strtoumax "inttypes.h" HAVE_STRTOUMAX)
check_symbol_exists(wmemchr "wchar.h" HAVE_WMEMCHR)
check_symbol_exists(AddDllDirectory "windows.h" HAVE_ADDDLLDIRECTORY)
check_symbol_exists(SetDefaultDllDirectories "windows.h"
HAVE_SETDEFAULTDLLDIRECTORIES)

5
defs.h
View File

@ -53,6 +53,11 @@ uintmax_t strtoumax(const char *nptr, char **endptr, int base);
#define SIZEu "zu"
#endif
#if !HAVE_WMEMCHR
/* Work around lack of wmemchr in older MSVC */
wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
#endif
#if defined __GNUC__ || defined __clang__
/*
* On MinGW, the correct compiler format checking for vsnprintf() etc

View File

@ -42,6 +42,9 @@ add_sources_from_current_dir(utils
if(NOT HAVE_STRTOUMAX)
add_sources_from_current_dir(utils utils/strtoumax.c)
endif()
if(NOT HAVE_WMEMCHR)
add_sources_from_current_dir(utils utils/wmemchr.c)
endif()
add_sources_from_current_dir(eventloop
cliloop.c handle-wait.c)
add_sources_from_current_dir(console

View File

@ -4,6 +4,8 @@
* string is dynamically allocated; caller must free.
*/
#include <wchar.h>
#include "putty.h"
char *GetDlgItemText_alloc(HWND hwnd, int id)
@ -27,7 +29,7 @@ wchar_t *GetDlgItemTextW_alloc(HWND hwnd, int id)
do {
sgrowarray_nm(ret, size, size);
GetDlgItemTextW(hwnd, id, ret, size);
} while (!memchr(ret, '\0', size-1));
} while (!wmemchr(ret, L'\0', size-1));
return ret;
}

15
windows/utils/wmemchr.c Normal file
View File

@ -0,0 +1,15 @@
/*
* Work around lack of wmemchr in older MSVC libraries.
*/
#include <wchar.h>
#include "defs.h"
wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n)
{
for (; n != 0; s++, n--)
if (*s == c)
return (wchar_t *)s;
return NULL;
}