1- From f39c0ccf0b8af918d16b8072cde116a4b33d32bb Mon Sep 17 00:00:00 2001
1+ From c7fc33d0f7541706048424b28292996690f5f1c1 Mon Sep 17 00:00:00 2001
22From: =?UTF-8?q?Hugo=20Beauz=C3=A9e-Luyssen?= <hugo@beauzee.fr>
33Date: Mon, 27 Jan 2025 08:36:07 +0100
44Subject: [PATCH] crypto: fix preprocessor concatenation
@@ -8,8 +8,8 @@ failures:
88crypto/defaults.c:kepi:23: error: pasting ""SOFTWARE\\WOW6432Node\\OpenSSL"" and ""-"" does not give a valid preprocessing token
99---
1010 crypto/cversion.c | 2 +-
11- crypto/defaults.c | 10 +++++-----
12- 2 files changed, 6 insertions(+), 6 deletions(-)
11+ crypto/defaults.c | 39 +++++++++++++++++++++------------- -----
12+ 2 files changed, 22 insertions(+), 19 deletions(-)
1313
1414diff --git a/crypto/cversion.c b/crypto/cversion.c
1515index 87154645b0..ae439c668b 100644
@@ -25,7 +25,7 @@ index 87154645b0..ae439c668b 100644
2525 #endif
2626
2727diff --git a/crypto/defaults.c b/crypto/defaults.c
28- index 908539cf31..09ac5a8799 100644
28+ index 908539cf31..b649e90258 100644
2929--- a/crypto/defaults.c
3030+++ b/crypto/defaults.c
3131@@ -19,7 +19,7 @@
@@ -37,31 +37,81 @@ index 908539cf31..09ac5a8799 100644
3737 # endif
3838
3939 /**
40- @@ -69,8 +69,8 @@ static char *get_windows_regdirs(char *dst, LPCTSTR valuename)
40+ @@ -60,44 +60,47 @@ static char *modulesdirptr = NULL;
41+ *
42+ * @return A pointer to a char array containing the registry directories.
43+ */
44+ - static char *get_windows_regdirs(char *dst, LPCTSTR valuename)
45+ + static char *get_windows_regdirs(char *dst, DWORD dstsizebytes, LPCWSTR valuename)
46+ {
47+ char *retval = NULL;
48+ # ifdef REGISTRY_KEY
49+ - DWORD keysize;
50+ + DWORD keysizebytes;
51+ DWORD ktype;
4152 HKEY hkey;
4253 LSTATUS ret;
4354 DWORD index = 0;
4455- LPCTCH tempstr = NULL;
4556-
46- + LPCTSTR tempstr = NULL;
57+ + LPCWSTR tempstr = NULL;
4758+
4859 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
4960 TEXT(REGISTRY_KEY), KEY_WOW64_32KEY,
5061 KEY_QUERY_VALUE, &hkey);
51- @@ -93,11 +93,11 @@ static char *get_windows_regdirs(char *dst, LPCTSTR valuename )
62+ if (ret != ERROR_SUCCESS )
5263 goto out;
5364
54- if (RegQueryValueEx(hkey, valuename,
65+ - ret = RegQueryValueEx(hkey, valuename, NULL, &ktype, NULL,
66+ - &keysize);
67+ + // Always use wide call so we can avoid extra encoding conversions on the output
68+ + ret = RegQueryValueExW(hkey, valuename, NULL, &ktype, NULL,
69+ + &keysizebytes);
70+ if (ret != ERROR_SUCCESS)
71+ goto out;
72+ - if (ktype != REG_EXPAND_SZ)
73+ + if (ktype != REG_EXPAND_SZ && ktype != REG_SZ)
74+ goto out;
75+ - if (keysize > MAX_PATH)
76+ + if (keysizebytes > MAX_PATH*sizeof(WCHAR))
77+ goto out;
78+
79+ - keysize++;
80+ - tempstr = OPENSSL_zalloc(keysize * sizeof(TCHAR));
81+ + // RegQueryValueExW does not guarantee the buffer is null terminated,
82+ + // so we make space for one
83+ + keysizebytes += sizeof(WCHAR);
84+ + tempstr = OPENSSL_zalloc(keysizebytes);
85+
86+ if (tempstr == NULL)
87+ goto out;
88+
89+ - if (RegQueryValueEx(hkey, valuename,
5590- NULL, &ktype, tempstr, &keysize) != ERROR_SUCCESS)
56- + NULL, &ktype, (LPBYTE)tempstr, &keysize) != ERROR_SUCCESS)
91+ + if (RegQueryValueExW(hkey, valuename,
92+ + NULL, &ktype, (LPBYTE)tempstr, &keysizebytes) != ERROR_SUCCESS)
5793 goto out;
5894
59- if (!WideCharToMultiByte(CP_UTF8, 0, tempstr, -1, dst, keysize,
95+ - if (!WideCharToMultiByte(CP_UTF8, 0, tempstr, -1, dst, keysize,
6096- NULL, NULL))
97+ + if (!WideCharToMultiByte(CP_UTF8, 0, tempstr, -1, dst, dstsizebytes,
6198+ NULL, NULL))
6299 goto out;
63100
64101 retval = dst;
102+ @@ -117,9 +120,9 @@ static CRYPTO_ONCE defaults_setup_init = CRYPTO_ONCE_STATIC_INIT;
103+ */
104+ DEFINE_RUN_ONCE_STATIC(do_defaults_setup)
105+ {
106+ - get_windows_regdirs(openssldir, TEXT("OPENSSLDIR"));
107+ - get_windows_regdirs(enginesdir, TEXT("ENGINESDIR"));
108+ - get_windows_regdirs(modulesdir, TEXT("MODULESDIR"));
109+ + get_windows_regdirs(openssldir, sizeof(openssldir), L"OPENSSLDIR");
110+ + get_windows_regdirs(enginesdir, sizeof(enginesdir), L"ENGINESDIR");
111+ + get_windows_regdirs(modulesdir, sizeof(modulesdir), L"MODULESDIR");
112+
113+ /*
114+ * Set our pointers only if the directories are fetched properly
65115- -
661162.34.1
67117
0 commit comments