https://git.reactos.org/?p=reactos.git;a=commitdiff;h=8532f1874a34ae41818a7…
commit 8532f1874a34ae41818a770f8c8bd8a77b70e21a
Author: Katayama Hirofumi MZ <katayama.hirofumi.mz(a)gmail.com>
AuthorDate: Mon Aug 7 22:55:49 2023 +0900
Commit: GitHub <noreply(a)github.com>
CommitDate: Mon Aug 7 22:55:49 2023 +0900
[USRMGR] Get text correctly even if length is zero (#5543)
- Add GetDlgItemTextAlloc and
GetComboBoxLBTextAlloc helper functions.
- Get the text data correctly even if
the text length is zero.
CORE-18275
---
dll/cpl/usrmgr/groupprops.c | 17 +------
dll/cpl/usrmgr/groups.c | 23 +++------
dll/cpl/usrmgr/userprops.c | 115 ++++++++------------------------------------
dll/cpl/usrmgr/users.c | 56 +++++----------------
dll/cpl/usrmgr/usrmgr.c | 20 +++++++-
dll/cpl/usrmgr/usrmgr.h | 4 +-
6 files changed, 62 insertions(+), 173 deletions(-)
diff --git a/dll/cpl/usrmgr/groupprops.c b/dll/cpl/usrmgr/groupprops.c
index ec82e1e1d2a..0ad86be8745 100644
--- a/dll/cpl/usrmgr/groupprops.c
+++ b/dll/cpl/usrmgr/groupprops.c
@@ -494,23 +494,11 @@ SetGeneralGroupData(HWND hwndDlg,
PGENERAL_GROUP_DATA pGroupData)
{
LOCALGROUP_INFO_1 groupInfo;
- LPTSTR pszComment = NULL;
- INT nLength;
NET_API_STATUS status;
DWORD dwIndex;
/* Get the group description */
- nLength = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_GROUP_GENERAL_DESCRIPTION));
- if (nLength == 0)
- {
- groupInfo.lgrpi1_comment = NULL;
- }
- else
- {
- pszComment = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
- GetDlgItemText(hwndDlg, IDC_GROUP_GENERAL_DESCRIPTION, pszComment, nLength + 1);
- groupInfo.lgrpi1_comment = pszComment;
- }
+ groupInfo.lgrpi1_comment = GetDlgItemTextAlloc(hwndDlg,
IDC_GROUP_GENERAL_DESCRIPTION);
status = NetLocalGroupSetInfo(NULL, pGroupData->szGroupName, 1,
(LPBYTE)&groupInfo, &dwIndex);
if (status != NERR_Success)
@@ -518,8 +506,7 @@ SetGeneralGroupData(HWND hwndDlg,
ERR("NetLocalGroupSetInfo failed. Status: %lu Index: %lu", status,
dwIndex);
}
- if (pszComment)
- HeapFree(GetProcessHeap(), 0, pszComment);
+ HeapFree(GetProcessHeap(), 0, groupInfo.lgrpi1_comment);
return TRUE;
}
diff --git a/dll/cpl/usrmgr/groups.c b/dll/cpl/usrmgr/groups.c
index 44423038b84..cf03624731b 100644
--- a/dll/cpl/usrmgr/groups.c
+++ b/dll/cpl/usrmgr/groups.c
@@ -155,19 +155,11 @@ NewGroupDlgProc(HWND hwndDlg,
break;
}
- nLength = SendDlgItemMessage(hwndDlg, IDC_GROUP_NEW_NAME,
WM_GETTEXTLENGTH, 0, 0);
- if (nLength > 0)
- {
- groupInfo->lgrpi1_name = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
- GetDlgItemText(hwndDlg, IDC_GROUP_NEW_NAME,
groupInfo->lgrpi1_name, nLength + 1);
- }
+ /* Get Name */
+ groupInfo->lgrpi1_name = GetDlgItemTextAlloc(hwndDlg,
IDC_GROUP_NEW_NAME);
- nLength = SendDlgItemMessage(hwndDlg, IDC_GROUP_NEW_DESCRIPTION,
WM_GETTEXTLENGTH, 0, 0);
- if (nLength > 0)
- {
- groupInfo->lgrpi1_comment = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
- GetDlgItemText(hwndDlg, IDC_GROUP_NEW_DESCRIPTION,
groupInfo->lgrpi1_comment, nLength + 1);
- }
+ /* Get Description */
+ groupInfo->lgrpi1_comment = GetDlgItemTextAlloc(hwndDlg,
IDC_GROUP_NEW_DESCRIPTION);
EndDialog(hwndDlg, IDOK);
break;
@@ -228,11 +220,8 @@ GroupNew(HWND hwndDlg)
group.lgrpi1_comment);
}
- if (group.lgrpi1_name)
- HeapFree(GetProcessHeap(), 0, group.lgrpi1_name);
-
- if (group.lgrpi1_comment)
- HeapFree(GetProcessHeap(), 0, group.lgrpi1_comment);
+ HeapFree(GetProcessHeap(), 0, group.lgrpi1_name);
+ HeapFree(GetProcessHeap(), 0, group.lgrpi1_comment);
}
diff --git a/dll/cpl/usrmgr/userprops.c b/dll/cpl/usrmgr/userprops.c
index bc013522714..773b571d158 100644
--- a/dll/cpl/usrmgr/userprops.c
+++ b/dll/cpl/usrmgr/userprops.c
@@ -87,80 +87,38 @@ SetUserProfileData(HWND hwndDlg,
PPROFILE_USER_DATA pUserData)
{
PUSER_INFO_3 pUserInfo = NULL;
- LPTSTR pszProfilePath = NULL;
- LPTSTR pszScriptPath = NULL;
- LPTSTR pszHomeDir = NULL;
- LPTSTR pszHomeDrive = NULL;
NET_API_STATUS status;
DWORD dwIndex;
- INT nLength;
INT nIndex;
NetUserGetInfo(NULL, pUserData->szUserName, 3, (LPBYTE*)&pUserInfo);
/* Get the profile path */
- nLength = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_USER_PROFILE_PATH));
- if (nLength == 0)
- {
- pUserInfo->usri3_profile = NULL;
- }
- else
- {
- pszProfilePath = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_PROFILE_PATH, pszProfilePath, nLength + 1);
- pUserInfo->usri3_profile = pszProfilePath;
- }
+ pUserInfo->usri3_profile = GetDlgItemTextAlloc(hwndDlg, IDC_USER_PROFILE_PATH);
/* Get the script path */
- nLength = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_USER_PROFILE_SCRIPT));
- if (nLength == 0)
- {
- pUserInfo->usri3_script_path = NULL;
- }
- else
- {
- pszScriptPath = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_PROFILE_SCRIPT, pszScriptPath, nLength + 1);
- pUserInfo->usri3_script_path = pszScriptPath;
- }
+ pUserInfo->usri3_script_path = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_PROFILE_SCRIPT);
if (IsDlgButtonChecked(hwndDlg, IDC_USER_PROFILE_LOCAL) == BST_CHECKED)
{
/* Local home directory */
- nLength = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_USER_PROFILE_LOCAL_PATH));
- if (nLength == 0)
- {
- pUserInfo->usri3_home_dir = NULL;
- }
- else
- {
- pszHomeDir = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_PROFILE_LOCAL_PATH, pszHomeDir, nLength +
1);
- pUserInfo->usri3_home_dir = pszHomeDir;
- }
+ pUserInfo->usri3_home_dir = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_PROFILE_LOCAL_PATH);
}
else
{
/* Remote home directory */
- nLength = GetWindowTextLength(GetDlgItem(hwndDlg,
IDC_USER_PROFILE_REMOTE_PATH));
- if (nLength == 0)
- {
- pUserInfo->usri3_home_dir = NULL;
- }
- else
- {
- pszHomeDir = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_PROFILE_REMOTE_PATH, pszHomeDir, nLength +
1);
- pUserInfo->usri3_home_dir = pszHomeDir;
- }
+ pUserInfo->usri3_home_dir = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_PROFILE_REMOTE_PATH);
nIndex = SendMessage(GetDlgItem(hwndDlg, IDC_USER_PROFILE_DRIVE), CB_GETCURSEL,
0, 0);
if (nIndex != CB_ERR)
{
- nLength = SendMessage(GetDlgItem(hwndDlg, IDC_USER_PROFILE_DRIVE),
CB_GETLBTEXTLEN, nIndex, 0);
- pszHomeDrive = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) *
sizeof(TCHAR));
- SendMessage(GetDlgItem(hwndDlg, IDC_USER_PROFILE_DRIVE), CB_GETLBTEXT,
nIndex, (LPARAM)pszHomeDrive);
- pUserInfo->usri3_home_dir_drive = pszHomeDrive;
+ pUserInfo->usri3_home_dir_drive =
+ GetComboBoxLBTextAlloc(hwndDlg, IDC_USER_PROFILE_DRIVE, nIndex);
+ }
+ else
+ {
+ pUserInfo->usri3_home_dir_drive =
+ HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TCHAR));
}
}
@@ -170,17 +128,10 @@ SetUserProfileData(HWND hwndDlg,
ERR("NetUserSetInfo failed. Status: %lu Index: %lu", status,
dwIndex);
}
- if (pszProfilePath)
- HeapFree(GetProcessHeap(), 0, pszProfilePath);
-
- if (pszScriptPath)
- HeapFree(GetProcessHeap(), 0, pszScriptPath);
-
- if (pszHomeDir)
- HeapFree(GetProcessHeap(), 0, pszHomeDir);
-
- if (pszHomeDrive)
- HeapFree(GetProcessHeap(), 0, pszHomeDrive);
+ HeapFree(GetProcessHeap(), 0, pUserInfo->usri3_profile);
+ HeapFree(GetProcessHeap(), 0, pUserInfo->usri3_script_path);
+ HeapFree(GetProcessHeap(), 0, pUserInfo->usri3_home_dir);
+ HeapFree(GetProcessHeap(), 0, pUserInfo->usri3_home_dir_drive);
NetApiBufferFree(pUserInfo);
@@ -743,11 +694,8 @@ SetUserGeneralData(HWND hwndDlg,
PGENERAL_USER_DATA pUserData)
{
PUSER_INFO_3 pUserInfo = NULL;
- LPTSTR pszFullName = NULL;
- LPTSTR pszComment = NULL;
NET_API_STATUS status;
DWORD dwIndex;
- INT nLength;
NetUserGetInfo(NULL, pUserData->szUserName, 3, (LPBYTE*)&pUserInfo);
@@ -757,29 +705,11 @@ SetUserGeneralData(HWND hwndDlg,
pUserInfo->usri3_password_expired = pUserData->dwPasswordExpired;
- nLength = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_USER_GENERAL_FULL_NAME));
- if (nLength == 0)
- {
- pUserInfo->usri3_full_name = NULL;
- }
- else
- {
- pszFullName = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_GENERAL_FULL_NAME, pszFullName, nLength + 1);
- pUserInfo->usri3_full_name = pszFullName;
- }
+ /* Get full name */
+ pUserInfo->usri3_full_name = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_GENERAL_FULL_NAME);
- nLength = GetWindowTextLength(GetDlgItem(hwndDlg, IDC_USER_GENERAL_DESCRIPTION));
- if (nLength == 0)
- {
- pUserInfo->usri3_full_name = NULL;
- }
- else
- {
- pszComment = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_GENERAL_DESCRIPTION, pszComment, nLength + 1);
- pUserInfo->usri3_comment = pszComment;
- }
+ /* Get desciption */
+ pUserInfo->usri3_comment = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_GENERAL_DESCRIPTION);
status = NetUserSetInfo(NULL, pUserData->szUserName, 3, (LPBYTE)pUserInfo,
&dwIndex);
if (status != NERR_Success)
@@ -787,11 +717,8 @@ SetUserGeneralData(HWND hwndDlg,
ERR("NetUserSetInfo failed. Status: %lu Index: %lu", status,
dwIndex);
}
- if (pszFullName)
- HeapFree(GetProcessHeap(), 0, pszFullName);
-
- if (pszComment)
- HeapFree(GetProcessHeap(), 0, pszComment);
+ HeapFree(GetProcessHeap(), 0, pUserInfo->usri3_full_name);
+ HeapFree(GetProcessHeap(), 0, pUserInfo->usri3_comment);
NetApiBufferFree(pUserInfo);
diff --git a/dll/cpl/usrmgr/users.c b/dll/cpl/usrmgr/users.c
index 084743e72f8..5c785b6e48d 100644
--- a/dll/cpl/usrmgr/users.c
+++ b/dll/cpl/usrmgr/users.c
@@ -60,7 +60,6 @@ ChangePasswordDlgProc(HWND hwndDlg,
LPARAM lParam)
{
PUSER_INFO_1003 userInfo;
- INT nLength;
UNREFERENCED_PARAMETER(wParam);
@@ -79,14 +78,9 @@ ChangePasswordDlgProc(HWND hwndDlg,
case IDOK:
if (CheckPasswords(hwndDlg, IDC_EDIT_PASSWORD1, IDC_EDIT_PASSWORD2))
{
-
/* Store the password */
- nLength = SendDlgItemMessage(hwndDlg, IDC_EDIT_PASSWORD1,
WM_GETTEXTLENGTH, 0, 0);
- if (nLength > 0)
- {
- userInfo->usri1003_password = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
- GetDlgItemText(hwndDlg, IDC_EDIT_PASSWORD1,
userInfo->usri1003_password, nLength + 1);
- }
+ userInfo->usri1003_password =
+ GetDlgItemTextAlloc(hwndDlg, IDC_EDIT_PASSWORD1);
EndDialog(hwndDlg, IDOK);
}
@@ -147,8 +141,7 @@ UserChangePassword(HWND hwndDlg)
}
}
- if (user.usri1003_password)
- HeapFree(GetProcessHeap(), 0, user.usri1003_password);
+ HeapFree(GetProcessHeap(), 0, user.usri1003_password);
}
@@ -250,36 +243,16 @@ NewUserDlgProc(HWND hwndDlg,
}
/* Store the user name */
- nLength = SendDlgItemMessage(hwndDlg, IDC_USER_NEW_NAME,
WM_GETTEXTLENGTH, 0, 0);
- if (nLength > 0)
- {
- userInfo->usri3_name = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_NEW_NAME,
userInfo->usri3_name, nLength + 1);
- }
+ userInfo->usri3_name = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_NEW_NAME);
/* Store the full user name */
- nLength = SendDlgItemMessage(hwndDlg, IDC_USER_NEW_FULL_NAME,
WM_GETTEXTLENGTH, 0, 0);
- if (nLength > 0)
- {
- userInfo->usri3_full_name = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_NEW_FULL_NAME,
userInfo->usri3_full_name, nLength + 1);
- }
+ userInfo->usri3_full_name = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_NEW_FULL_NAME);
/* Store the description */
- nLength = SendDlgItemMessage(hwndDlg, IDC_USER_NEW_DESCRIPTION,
WM_GETTEXTLENGTH, 0, 0);
- if (nLength > 0)
- {
- userInfo->usri3_comment = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_NEW_DESCRIPTION,
userInfo->usri3_comment, nLength + 1);
- }
+ userInfo->usri3_comment = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_NEW_DESCRIPTION);
/* Store the password */
- nLength = SendDlgItemMessage(hwndDlg, IDC_USER_NEW_PASSWORD1,
WM_GETTEXTLENGTH, 0, 0);
- if (nLength > 0)
- {
- userInfo->usri3_password = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, (nLength + 1) * sizeof(WCHAR));
- GetDlgItemText(hwndDlg, IDC_USER_NEW_PASSWORD1,
userInfo->usri3_password, nLength + 1);
- }
+ userInfo->usri3_password = GetDlgItemTextAlloc(hwndDlg,
IDC_USER_NEW_PASSWORD1);
EndDialog(hwndDlg, IDOK);
break;
@@ -351,17 +324,10 @@ UserNew(HWND hwndDlg)
user.usri3_comment);
}
- if (user.usri3_name)
- HeapFree(GetProcessHeap(), 0, user.usri3_name);
-
- if (user.usri3_full_name)
- HeapFree(GetProcessHeap(), 0, user.usri3_full_name);
-
- if (user.usri3_comment)
- HeapFree(GetProcessHeap(), 0, user.usri3_comment);
-
- if (user.usri3_password)
- HeapFree(GetProcessHeap(), 0, user.usri3_password);
+ HeapFree(GetProcessHeap(), 0, user.usri3_name);
+ HeapFree(GetProcessHeap(), 0, user.usri3_full_name);
+ HeapFree(GetProcessHeap(), 0, user.usri3_comment);
+ HeapFree(GetProcessHeap(), 0, user.usri3_password);
}
diff --git a/dll/cpl/usrmgr/usrmgr.c b/dll/cpl/usrmgr/usrmgr.c
index 6c00abd666e..cd77052eeb7 100644
--- a/dll/cpl/usrmgr/usrmgr.c
+++ b/dll/cpl/usrmgr/usrmgr.c
@@ -5,6 +5,7 @@
* PURPOSE: Main functions
*
* PROGRAMMERS: Eric Kohl
+ * Katayama Hirofumi MZ <katayama.hirofumi.mz(a)gmail.com>
*/
#include "usrmgr.h"
@@ -15,6 +16,24 @@ static LONG APIENTRY UsrmgrApplet(HWND hwnd, UINT uMsg, LPARAM wParam,
LPARAM lP
HINSTANCE hApplet = 0;
+LPTSTR GetDlgItemTextAlloc(HWND hwndDlg, INT nDlgItem)
+{
+ INT nLength = GetWindowTextLength(GetDlgItem(hwndDlg, nDlgItem));
+ LPTSTR psz = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
+ if (psz)
+ GetDlgItemText(hwndDlg, nDlgItem, psz, nLength + 1);
+ return psz;
+}
+
+LPTSTR GetComboBoxLBTextAlloc(HWND hwndDlg, INT nDlgItem, INT nIndex)
+{
+ INT nLength = (INT)SendDlgItemMessage(hwndDlg, nDlgItem, CB_GETLBTEXTLEN, nIndex,
0);
+ LPTSTR psz = HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(TCHAR));
+ if (psz)
+ SendDlgItemMessage(hwndDlg, nDlgItem, CB_GETLBTEXT, nIndex, (LPARAM)psz);
+ return psz;
+}
+
/* Applets */
APPLET Applets[NUM_APPLETS] =
{
@@ -26,7 +45,6 @@ APPLET Applets[NUM_APPLETS] =
}
};
-
static VOID
InitPropSheetPage(PROPSHEETPAGE *psp, WORD idDlg, DLGPROC DlgProc)
{
diff --git a/dll/cpl/usrmgr/usrmgr.h b/dll/cpl/usrmgr/usrmgr.h
index 5f9c12ea191..c53e47cce8a 100644
--- a/dll/cpl/usrmgr/usrmgr.h
+++ b/dll/cpl/usrmgr/usrmgr.h
@@ -35,11 +35,13 @@ typedef struct _APPLET
extern HINSTANCE hApplet;
-
INT_PTR CALLBACK UsersPageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK GroupsPageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK ExtraPageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
+LPTSTR GetDlgItemTextAlloc(HWND hwndDlg, INT nDlgItem);
+LPTSTR GetComboBoxLBTextAlloc(HWND hwndDlg, INT nDlgItem, INT nIndex);
+
/* groupprops.c */
BOOL
GroupProperties(HWND hwndDlg);