Correct arguments to GetDlgItemText().
authorIain Patterson <me@iain.cx>
Sat, 21 Dec 2013 15:33:23 +0000 (15:33 +0000)
committerIain Patterson <me@iain.cx>
Sat, 21 Dec 2013 15:33:23 +0000 (15:33 +0000)
The fourth argument to GetDlgItemText() is a character count not a
buffer size.

gui.cpp

diff --git a/gui.cpp b/gui.cpp
index 1459d97..bef4d4e 100644 (file)
--- a/gui.cpp
+++ b/gui.cpp
@@ -74,11 +74,11 @@ static inline void check_method_timeout(HWND tab, unsigned long control, unsigne
   if (translated) *timeout = configured;\r
 }\r
 \r
-static inline void check_io(TCHAR *name, TCHAR *buffer, size_t bufsize, unsigned long control) {\r
+static inline void check_io(TCHAR *name, TCHAR *buffer, unsigned long len, unsigned long control) {\r
   if (! SendMessage(GetDlgItem(tablist[NSSM_TAB_IO], control), WM_GETTEXTLENGTH, 0, 0)) return;\r
-  if (GetDlgItemText(tablist[NSSM_TAB_IO], control, buffer, (int) bufsize)) return;\r
+  if (GetDlgItemText(tablist[NSSM_TAB_IO], control, buffer, (int) len)) return;\r
   popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_MESSAGE_PATH_TOO_LONG, name);\r
-  ZeroMemory(buffer, bufsize);\r
+  ZeroMemory(buffer, len * sizeof(TCHAR));\r
 }\r
 \r
 /* Install the service. */\r
@@ -90,27 +90,27 @@ int install(HWND window) {
     set_nssm_service_defaults(service);\r
 \r
     /* Get service name. */\r
-    if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) {\r
+    if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {\r
       popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);\r
       cleanup_nssm_service(service);\r
       return 2;\r
     }\r
 \r
     /* Get executable name */\r
-    if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe, sizeof(service->exe))) {\r
+    if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_PATH, service->exe, _countof(service->exe))) {\r
       popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_PATH);\r
       return 3;\r
     }\r
 \r
     /* Get startup directory. */\r
-    if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir, sizeof(service->dir))) {\r
+    if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_DIR, service->dir, _countof(service->dir))) {\r
       _sntprintf_s(service->dir, _countof(service->dir), _TRUNCATE, _T("%s"), service->exe);\r
       strip_basename(service->dir);\r
     }\r
 \r
     /* Get flags. */\r
     if (SendMessage(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {\r
-      if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags, sizeof(service->flags))) {\r
+      if (! GetDlgItemText(tablist[NSSM_TAB_APPLICATION], IDC_FLAGS, service->flags, _countof(service->flags))) {\r
         popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_INVALID_OPTIONS);\r
         return 4;\r
       }\r
@@ -132,9 +132,9 @@ int install(HWND window) {
     if (service->default_exit_action == CB_ERR) service->default_exit_action = 0;\r
 \r
     /* Get I/O stuff. */\r
-    check_io(_T("stdin"), service->stdin_path, sizeof(service->stdin_path), IDC_STDIN);\r
-    check_io(_T("stdout"), service->stdout_path, sizeof(service->stdout_path), IDC_STDOUT);\r
-    check_io(_T("stderr"), service->stderr_path, sizeof(service->stderr_path), IDC_STDERR);\r
+    check_io(_T("stdin"), service->stdin_path, _countof(service->stdin_path), IDC_STDIN);\r
+    check_io(_T("stdout"), service->stdout_path, _countof(service->stdout_path), IDC_STDOUT);\r
+    check_io(_T("stderr"), service->stderr_path, _countof(service->stderr_path), IDC_STDERR);\r
 \r
     /* Override stdout and/or stderr. */\r
     if (SendDlgItemMessage(tablist[NSSM_TAB_ROTATION], IDC_TRUNCATE, BM_GETCHECK, 0, 0) & BST_CHECKED) {\r
@@ -261,7 +261,7 @@ int remove(HWND window) {
   nssm_service_t *service = alloc_nssm_service();\r
   if (service) {\r
     /* Get service name */\r
-    if (! GetDlgItemText(window, IDC_NAME, service->name, sizeof(service->name))) {\r
+    if (! GetDlgItemText(window, IDC_NAME, service->name, _countof(service->name))) {\r
       popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_GUI_MISSING_SERVICE_NAME);\r
       cleanup_nssm_service(service);\r
       return 2;\r
@@ -385,12 +385,12 @@ INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
         /* Browse for application. */\r
         case IDC_BROWSE:\r
           dlg = GetDlgItem(tab, IDC_PATH);\r
-          GetDlgItemText(tab, IDC_PATH, buffer, sizeof(buffer));\r
+          GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));\r
           browse(dlg, buffer, OFN_FILEMUSTEXIST, NSSM_GUI_BROWSE_FILTER_APPLICATIONS, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);\r
           /* Fill in startup directory if it wasn't already specified. */\r
-          GetDlgItemText(tab, IDC_DIR, buffer, sizeof(buffer));\r
+          GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));\r
           if (! buffer[0]) {\r
-            GetDlgItemText(tab, IDC_PATH, buffer, sizeof(buffer));\r
+            GetDlgItemText(tab, IDC_PATH, buffer, _countof(buffer));\r
             strip_basename(buffer);\r
             SetDlgItemText(tab, IDC_DIR, buffer);\r
           }\r
@@ -399,26 +399,26 @@ INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
           /* Browse for startup directory. */\r
         case IDC_BROWSE_DIR:\r
           dlg = GetDlgItem(tab, IDC_DIR);\r
-          GetDlgItemText(tab, IDC_DIR, buffer, sizeof(buffer));\r
+          GetDlgItemText(tab, IDC_DIR, buffer, _countof(buffer));\r
           browse(dlg, buffer, OFN_NOVALIDATE, NSSM_GUI_BROWSE_FILTER_DIRECTORIES, 0);\r
           break;\r
 \r
         /* Browse for stdin. */\r
         case IDC_BROWSE_STDIN:\r
           dlg = GetDlgItem(tab, IDC_STDIN);\r
-          GetDlgItemText(tab, IDC_STDIN, buffer, sizeof(buffer));\r
+          GetDlgItemText(tab, IDC_STDIN, buffer, _countof(buffer));\r
           browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);\r
           break;\r
 \r
         /* Browse for stdout. */\r
         case IDC_BROWSE_STDOUT:\r
           dlg = GetDlgItem(tab, IDC_STDOUT);\r
-          GetDlgItemText(tab, IDC_STDOUT, buffer, sizeof(buffer));\r
+          GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));\r
           browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);\r
           /* Fill in stderr if it wasn't already specified. */\r
-          GetDlgItemText(tab, IDC_STDERR, buffer, sizeof(buffer));\r
+          GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));\r
           if (! buffer[0]) {\r
-            GetDlgItemText(tab, IDC_STDOUT, buffer, sizeof(buffer));\r
+            GetDlgItemText(tab, IDC_STDOUT, buffer, _countof(buffer));\r
             SetDlgItemText(tab, IDC_STDERR, buffer);\r
           }\r
           break;\r
@@ -426,7 +426,7 @@ INT_PTR CALLBACK tab_dlg(HWND tab, UINT message, WPARAM w, LPARAM l) {
         /* Browse for stderr. */\r
         case IDC_BROWSE_STDERR:\r
           dlg = GetDlgItem(tab, IDC_STDERR);\r
-          GetDlgItemText(tab, IDC_STDERR, buffer, sizeof(buffer));\r
+          GetDlgItemText(tab, IDC_STDERR, buffer, _countof(buffer));\r
           browse(dlg, buffer, 0, NSSM_GUI_BROWSE_FILTER_ALL_FILES, 0);\r
           break;\r
 \r