summaryrefslogtreecommitdiff
path: root/evutil.c
diff options
context:
space:
mode:
authorzhenhaonong <zhenhaonong@tencent.com>2022-09-16 15:55:56 +0800
committerAzat Khuzhin <a3at.mail@gmail.com>2022-09-26 21:43:21 +0300
commitf8bb9d84845be12b3ffb709bf9a26df4f40f898f (patch)
tree3fbe8a0f71a503bc43a45cfebf2d6edf0c574513 /evutil.c
parent211c6653ae78e6cf79bfdb30cbbcdd0376345751 (diff)
downloadlibevent-f8bb9d84845be12b3ffb709bf9a26df4f40f898f.tar.gz
Fix socketpair failure when temporary directory has non-latin character
Diffstat (limited to 'evutil.c')
-rw-r--r--evutil.c33
1 files changed, 20 insertions, 13 deletions
diff --git a/evutil.c b/evutil.c
index c321eb60..ae862ee1 100644
--- a/evutil.c
+++ b/evutil.c
@@ -31,6 +31,7 @@
#include <winsock2.h>
#include <winerror.h>
#include <ws2tcpip.h>
+#include <stringapiset.h>
#ifdef EVENT__HAVE_AFUNIX_H
#include <afunix.h>
#endif
@@ -212,14 +213,14 @@ evutil_read_file_(const char *filename, char **content_out, size_t *len_out,
#ifdef _WIN32
static int
-create_tmpfile(char tmpfile[MAX_PATH])
+create_tmpfile(WCHAR tmpfile[MAX_PATH])
{
- char short_path[MAX_PATH] = {0};
- char long_path[MAX_PATH] = {0};
- char prefix[4] = {0};
- // GetTempFileNameA() uses up to the first three characters of the prefix
+ WCHAR short_path[MAX_PATH] = {0};
+ WCHAR long_path[MAX_PATH] = {0};
+ WCHAR prefix[4] = {0};
+ // GetTempFileNameW() uses up to the first three characters of the prefix
// and windows filesystems are case-insensitive
- const char *base32set = "abcdefghijklmnopqrstuvwxyz012345";
+ const WCHAR *base32set = L"abcdefghijklmnopqrstuvwxyz012345";
ev_uint16_t rnd;
evutil_secure_rng_get_bytes(&rnd, sizeof(rnd));
@@ -228,9 +229,9 @@ create_tmpfile(char tmpfile[MAX_PATH])
prefix[2] = base32set[(rnd >> 10) & 31];
prefix[3] = '\0';
- GetTempPathA(MAX_PATH, short_path);
- GetLongPathNameA(short_path, long_path, MAX_PATH);
- if (!GetTempFileNameA(long_path, prefix, 0, tmpfile)) {
+ GetTempPathW(MAX_PATH, short_path);
+ GetLongPathNameW(short_path, long_path, MAX_PATH);
+ if (!GetTempFileNameW(long_path, prefix, 0, tmpfile)) {
event_warnx("GetTempFileName failed: %d", EVUTIL_SOCKET_ERROR());
return -1;
}
@@ -271,7 +272,8 @@ evutil_win_socketpair_afunix(int family, int type, int protocol,
struct sockaddr_un listen_addr;
struct sockaddr_un connect_addr;
- char tmp_file[MAX_PATH] = {0};
+ WCHAR tmp_file[MAX_PATH] = {0};
+ char tmp_file_utf8[MAX_PATH] = {0};
ev_socklen_t size;
int saved_errno = -1;
@@ -289,9 +291,14 @@ evutil_win_socketpair_afunix(int family, int type, int protocol,
if (create_tmpfile(tmp_file)) {
goto tidy_up_and_fail;
}
- DeleteFileA(tmp_file);
+ DeleteFileW(tmp_file);
+
+ /* Windows requires `sun_path` to be encoded by UTF-8 */
+ WideCharToMultiByte(
+ CP_UTF8, 0, tmp_file, MAX_PATH, tmp_file_utf8, MAX_PATH, NULL, NULL);
+
listen_addr.sun_family = AF_UNIX;
- if (strlcpy(listen_addr.sun_path, tmp_file, UNIX_PATH_MAX) >=
+ if (strlcpy(listen_addr.sun_path, tmp_file_utf8, UNIX_PATH_MAX) >=
UNIX_PATH_MAX) {
event_warnx("Temp file name is too long");
goto tidy_up_and_fail;
@@ -352,7 +359,7 @@ evutil_win_socketpair_afunix(int family, int type, int protocol,
if (acceptor != -1)
evutil_closesocket(acceptor);
if (tmp_file[0])
- DeleteFileA(tmp_file);
+ DeleteFileW(tmp_file);
EVUTIL_SET_SOCKET_ERROR(saved_errno);
return -1;