[USRMGR] Get text correctly even if length is zero (#5543)
authorKatayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
Mon, 7 Aug 2023 13:55:49 +0000 (22:55 +0900)
committerGitHub <noreply@github.com>
Mon, 7 Aug 2023 13:55:49 +0000 (22:55 +0900)
- 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
dll/cpl/usrmgr/groups.c
dll/cpl/usrmgr/userprops.c
dll/cpl/usrmgr/users.c
dll/cpl/usrmgr/usrmgr.c
dll/cpl/usrmgr/usrmgr.h

index ec82e1e..0ad86be 100644 (file)
@@ -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;
 }
index 4442303..cf03624 100644 (file)
@@ -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);
 }
 
 
index bc01352..773b571 100644 (file)
@@ -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);
 
index 084743e..5c785b6 100644 (file)
@@ -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);
 }
 
 
index 6c00abd..cd77052 100644 (file)
@@ -5,6 +5,7 @@
  * PURPOSE:         Main functions
  *
  * PROGRAMMERS:     Eric Kohl
+ *                  Katayama Hirofumi MZ <katayama.hirofumi.mz@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)
 {
index 5f9c12e..c53e47c 100644 (file)
@@ -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);