Browse Source

Compiler food.

Silence any compiler warnings relating to the use of _(v)snprintf() -
not _(v)snprintf_s() - and type conversions.
Iain Patterson 9 years ago
parent
commit
ce9eb5d464
7 changed files with 42 additions and 41 deletions
  1. 3 3
      event.cpp
  2. 6 6
      gui.cpp
  3. 5 5
      io.cpp
  4. 4 4
      process.cpp
  5. 13 13
      registry.cpp
  6. 1 1
      registry.h
  7. 10 9
      service.cpp

+ 3 - 3
event.cpp

@@ -14,7 +14,7 @@ char *error_string(unsigned long error) {
   }
 
   if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) error_message, NSSM_ERROR_BUFSIZE, 0)) {
-    if (_snprintf(error_message, NSSM_ERROR_BUFSIZE, "system error %lu", error) < 0) return 0;
+    if (_snprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, "system error %lu", error) < 0) return 0;
   }
   return error_message;
 }
@@ -24,7 +24,7 @@ char *message_string(unsigned long error) {
   char *ret;
   if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) {
     ret = (char *) HeapAlloc(GetProcessHeap(), 0, 32);
-    if (_snprintf(ret, NSSM_ERROR_BUFSIZE, "system error %lu", error) < 0) return 0;
+    if (_snprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, "system error %lu", error) < 0) return 0;
   }
   return ret;
 }
@@ -76,7 +76,7 @@ int popup_message(unsigned int type, unsigned long id, ...) {
 
   char blurb[256];
   va_start(arg, id);
-  if (vsnprintf(blurb, sizeof(blurb), format, arg) < 0) {
+  if (vsnprintf_s(blurb, sizeof(blurb), _TRUNCATE, format, arg) < 0) {
     va_end(arg);
     LocalFree(format);
     return MessageBox(0, "Message %lu was supposed to go here!", NSSM, MB_OK | MB_ICONEXCLAMATION);

+ 6 - 6
gui.cpp

@@ -154,8 +154,8 @@ int remove(HWND window) {
 void browse(HWND window) {
   if (! window) return;
 
-  unsigned long bufsize = 256;
-  unsigned long len = bufsize;
+  size_t bufsize = 256;
+  size_t len = bufsize;
   OPENFILENAME ofn;
   ZeroMemory(&ofn, sizeof(ofn));
   ofn.lStructSize = sizeof(ofn);
@@ -164,18 +164,18 @@ void browse(HWND window) {
   if (ofn.lpstrFilter) {
     ZeroMemory((void *) ofn.lpstrFilter, bufsize);
     char *localised = message_string(NSSM_GUI_BROWSE_FILTER_APPLICATIONS);
-    _snprintf((char *) ofn.lpstrFilter, bufsize, localised);
+    _snprintf_s((char *) ofn.lpstrFilter, bufsize, _TRUNCATE, localised);
     /* "Applications" + NULL + "*.exe" + NULL */
     len = strlen(localised) + 1;
     LocalFree(localised);
-    _snprintf((char *) ofn.lpstrFilter + len, bufsize - len, "*.exe");
+    _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.exe");
     /* "All files" + NULL + "*.*" + NULL */
     len += 6;
     localised = message_string(NSSM_GUI_BROWSE_FILTER_ALL_FILES);
-    _snprintf((char *) ofn.lpstrFilter + len, bufsize - len, localised);
+    _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, localised);
     len += strlen(localised) + 1;
     LocalFree(localised);
-    _snprintf((char *) ofn.lpstrFilter + len, bufsize - len, "*.*");
+    _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.*");
     /* Remainder of the buffer is already zeroed */
   }
   ofn.lpstrFile = new char[MAX_PATH];

+ 5 - 5
io.cpp

@@ -5,7 +5,7 @@ int get_createfile_parameters(HKEY key, char *prefix, char *path, unsigned long
   char value[NSSM_STDIO_LENGTH];
 
   /* Path. */
-  if (_snprintf(value, sizeof(value), "%s", prefix) < 0) {
+  if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s", prefix) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, prefix, "get_createfile_parameters()", 0);
     return 1;
   }
@@ -15,7 +15,7 @@ int get_createfile_parameters(HKEY key, char *prefix, char *path, unsigned long
   }
 
   /* ShareMode. */
-  if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_SHARING) < 0) {
+  if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s%s", prefix, NSSM_REG_STDIO_SHARING) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_SHARING, "get_createfile_parameters()", 0);
     return 3;
   }
@@ -26,7 +26,7 @@ int get_createfile_parameters(HKEY key, char *prefix, char *path, unsigned long
   }
 
   /* CreationDisposition. */
-  if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_DISPOSITION) < 0) {
+  if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s%s", prefix, NSSM_REG_STDIO_DISPOSITION) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_DISPOSITION, "get_createfile_parameters()", 0);
     return 5;
   }
@@ -37,7 +37,7 @@ int get_createfile_parameters(HKEY key, char *prefix, char *path, unsigned long
   }
 
   /* Flags. */
-  if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_FLAGS) < 0) {
+  if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s%s", prefix, NSSM_REG_STDIO_FLAGS) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_FLAGS, "get_createfile_parameters()", 0);
     return 7;
   }
@@ -96,7 +96,7 @@ int get_output_handles(HKEY key, STARTUPINFO *si) {
   if (get_createfile_parameters(key, NSSM_REG_STDOUT, path, &sharing, FILE_SHARE_READ | FILE_SHARE_WRITE, &disposition, OPEN_ALWAYS, &flags, FILE_ATTRIBUTE_NORMAL)) return 3;
   if (path[0]) {
     /* Remember path for comparison with stderr. */
-    if (_snprintf(stdout_path, sizeof(stdout_path), "%s", path) < 0) {
+    if (_snprintf_s(stdout_path, sizeof(stdout_path), _TRUNCATE, "%s", path) < 0) {
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "stdout_path", "get_output_handles", 0);
       return 4;
     }

+ 4 - 4
process.cpp

@@ -39,7 +39,7 @@ int check_parent(char *service_name, PROCESSENTRY32 *pe, unsigned long ppid, FIL
   HANDLE process_handle = OpenProcess(PROCESS_QUERY_INFORMATION, false, pe->th32ProcessID);
   if (! process_handle) {
     char pid_string[16];
-    _snprintf(pid_string, sizeof(pid_string), "%d", pe->th32ProcessID);
+    _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%d", pe->th32ProcessID);
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid_string, service_name, error_string(GetLastError()), 0);
     return 2;
   }
@@ -238,8 +238,8 @@ void kill_process_tree(char *service_name, unsigned long stop_method, unsigned l
   if (! pid) return;
 
   char pid_string[16], code[16];
-  _snprintf(pid_string, sizeof(pid_string), "%d", pid);
-  _snprintf(code, sizeof(code), "%d", exitcode);
+  _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%d", pid);
+  _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", exitcode);
   log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILLING, service_name, pid_string, code, 0);
 
   /* Get a snapshot of all processes in the system. */
@@ -285,7 +285,7 @@ void kill_process_tree(char *service_name, unsigned long stop_method, unsigned l
   }
 
   char ppid_string[16];
-  _snprintf(ppid_string, sizeof(ppid_string), "%d", ppid);
+  _snprintf_s(ppid_string, sizeof(ppid_string), _TRUNCATE, "%d", ppid);
   log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid_string, ppid_string, service_name, 0);
   if (! kill_process(service_name, stop_method, process_handle, pid, exitcode)) {
     /* Maybe it already died. */

+ 13 - 13
registry.cpp

@@ -4,7 +4,7 @@ int create_messages() {
   HKEY key;
 
   char registry[KEY_LENGTH];
-  if (_snprintf(registry, sizeof(registry), "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", NSSM) < 0) {
+  if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", NSSM) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "eventlog registry", "create_messages()", 0);
     return 1;
   }
@@ -19,7 +19,7 @@ int create_messages() {
   GetModuleFileName(0, path, MAX_PATH);
 
   /* Try to register the module but don't worry so much on failure */
-  RegSetValueEx(key, "EventMessageFile", 0, REG_SZ, (const unsigned char *) path, strlen(path) + 1);
+  RegSetValueEx(key, "EventMessageFile", 0, REG_SZ, (const unsigned char *) path, (unsigned long) strlen(path) + 1);
   unsigned long types = EVENTLOG_INFORMATION_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_ERROR_TYPE;
   RegSetValueEx(key, "TypesSupported", 0, REG_DWORD, /*XXX*/(PBYTE) &types, sizeof(types));
 
@@ -29,7 +29,7 @@ int create_messages() {
 int create_parameters(char *service_name, char *exe, char *flags, char *dir) {
   /* Get registry */
   char registry[KEY_LENGTH];
-  if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {
+  if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY, service_name) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REGISTRY", "create_parameters()", 0);
     return 1;
   }
@@ -42,19 +42,19 @@ int create_parameters(char *service_name, char *exe, char *flags, char *dir) {
   }
 
   /* Try to create the parameters */
-  if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_EXPAND_SZ, (const unsigned char *) exe, strlen(exe) + 1) != ERROR_SUCCESS) {
+  if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_EXPAND_SZ, (const unsigned char *) exe, (unsigned long) strlen(exe) + 1) != ERROR_SUCCESS) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXE, error_string(GetLastError()), 0);
     RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
     RegCloseKey(key);
     return 3;
   }
-  if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_EXPAND_SZ, (const unsigned char *) flags, strlen(flags) + 1) != ERROR_SUCCESS) {
+  if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_EXPAND_SZ, (const unsigned char *) flags, (unsigned long) strlen(flags) + 1) != ERROR_SUCCESS) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_FLAGS, error_string(GetLastError()), 0);
     RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
     RegCloseKey(key);
     return 4;
   }
-  if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_EXPAND_SZ, (const unsigned char *) dir, strlen(dir) + 1) != ERROR_SUCCESS) {
+  if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_EXPAND_SZ, (const unsigned char *) dir, (unsigned long) strlen(dir) + 1) != ERROR_SUCCESS) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_DIR, error_string(GetLastError()), 0);
     RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);
     RegCloseKey(key);
@@ -70,7 +70,7 @@ int create_parameters(char *service_name, char *exe, char *flags, char *dir) {
 int create_exit_action(char *service_name, const char *action_string) {
   /* Get registry */
   char registry[KEY_LENGTH];
-  if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
+  if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REG_EXIT", "create_exit_action()", 0);
     return 1;
   }
@@ -90,7 +90,7 @@ int create_exit_action(char *service_name, const char *action_string) {
   }
 
   /* Create the default value */
-  if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, strlen(action_string) + 1) != ERROR_SUCCESS) {
+  if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, (unsigned long) strlen(action_string) + 1) != ERROR_SUCCESS) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXIT, error_string(GetLastError()), 0);
     RegCloseKey(key);
     return 3;
@@ -219,12 +219,12 @@ int get_number(HKEY key, char *value, unsigned long *number) {
   return get_number(key, value, number, true);
 }
 
-int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen, char **env, unsigned long *throttle_delay, unsigned long *stop_method, STARTUPINFO *si) {
+int get_parameters(char *service_name, char *exe, unsigned long exelen, char *flags, unsigned long flagslen, char *dir, unsigned long dirlen, char **env, unsigned long *throttle_delay, unsigned long *stop_method, STARTUPINFO *si) {
   unsigned long ret;
 
   /* Get registry */
   char registry[KEY_LENGTH];
-  if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {
+  if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY, service_name) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REGISTRY", "get_parameters()", 0);
     return 1;
   }
@@ -288,7 +288,7 @@ int get_parameters(char *service_name, char *exe, int exelen, char *flags, int f
     if (ret != ERROR_FILE_NOT_FOUND) {
       if (type != REG_DWORD) {
         char milliseconds[16];
-        _snprintf(milliseconds, sizeof(milliseconds), "%lu", NSSM_RESET_THROTTLE_RESTART);
+        _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%lu", NSSM_RESET_THROTTLE_RESTART);
         log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_THROTTLE, service_name, NSSM_REG_THROTTLE, milliseconds, 0);
       }
       else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_THROTTLE, error_string(GetLastError()), 0);
@@ -330,7 +330,7 @@ int get_exit_action(char *service_name, unsigned long *ret, unsigned char *actio
 
   /* Get registry */
   char registry[KEY_LENGTH];
-  if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
+  if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REG_EXIT", "get_exit_action()", 0);
     return 1;
   }
@@ -348,7 +348,7 @@ int get_exit_action(char *service_name, unsigned long *ret, unsigned char *actio
 
   char code[64];
   if (! ret) code[0] = '\0';
-  else if (_snprintf(code, sizeof(code), "%lu", *ret) < 0) {
+  else if (_snprintf_s(code, sizeof(code), _TRUNCATE, "%lu", *ret) < 0) {
     RegCloseKey(key);
     return get_exit_action(service_name, 0, action, default_action);
   }

+ 1 - 1
registry.h

@@ -25,7 +25,7 @@ int expand_parameter(HKEY, char *, char *, unsigned long, bool, bool);
 int expand_parameter(HKEY, char *, char *, unsigned long, bool);
 int get_number(HKEY, char *, unsigned long *, bool);
 int get_number(HKEY, char *, unsigned long *);
-int get_parameters(char *, char *, int, char *, int, char *, int, char **, unsigned long *, unsigned long *, STARTUPINFO *);
+int get_parameters(char *, char *, unsigned long, char *, unsigned long, char *, unsigned long, char **, unsigned long *, unsigned long *, STARTUPINFO *);
 int get_exit_action(char *, unsigned long *, unsigned char *, bool *);
 
 #endif

+ 10 - 9
service.cpp

@@ -105,7 +105,7 @@ int install_service(char *name, char *exe, char *flags) {
     print_message(stderr, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);
     return 3;
   }
-  if (_snprintf(command, sizeof(command), "\"%s\"", path) < 0) {
+  if (_snprintf_s(command, sizeof(command), _TRUNCATE, "\"%s\"", path) < 0) {
     print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY_FOR_IMAGEPATH);
     return 4;
   }
@@ -179,7 +179,7 @@ int remove_service(char *name) {
 
 /* Service initialisation */
 void WINAPI service_main(unsigned long argc, char **argv) {
-  if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) {
+  if (_snprintf_s(service_name, sizeof(service_name), _TRUNCATE, "%s", argv[0]) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "service_name", "service_main()", 0);
     return;
   }
@@ -262,7 +262,7 @@ int monitor_service() {
   int ret = start_service();
   if (ret) {
     char code[16];
-    _snprintf(code, sizeof(code), "%d", ret);
+    _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", ret);
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_START_SERVICE_FAILED, exe, service_name, ret, 0);
     return ret;
   }
@@ -300,7 +300,7 @@ void log_service_control(char *service_name, unsigned long control, bool handled
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0);
       return;
     }
-    if (_snprintf(text, 11, "0x%08x", control) < 0) {
+    if (_snprintf_s(text, 11, _TRUNCATE, "0x%08x", control) < 0) {
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0);
       HeapFree(GetProcessHeap(), 0, text);
       return;
@@ -383,7 +383,7 @@ int start_service() {
 
   /* Launch executable with arguments */
   char cmd[CMD_LENGTH];
-  if (_snprintf(cmd, sizeof(cmd), "\"%s\" %s", exe, flags) < 0) {
+  if (_snprintf_s(cmd, sizeof(cmd), _TRUNCATE, "\"%s\" %s", exe, flags) < 0) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);
     close_output_handles(&si);
     return stop_service(2, true, true);
@@ -391,7 +391,8 @@ int start_service() {
 
   throttle_restart();
 
-  bool inherit_handles = (si.dwFlags & STARTF_USESTDHANDLES);
+  bool inherit_handles = false;
+  if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;
   if (! CreateProcess(0, cmd, 0, 0, inherit_handles, 0, env, dir, &si, &pi)) {
     unsigned long error = GetLastError();
     if (error == ERROR_INVALID_PARAMETER && env) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service_name, exe, NSSM_REG_ENV, 0);
@@ -481,7 +482,7 @@ void CALLBACK end_service(void *arg, unsigned char why) {
     tree.  See below for the possible values of the why argument.
   */
   if (! why) {
-    _snprintf(code, sizeof(code), "%d", exitcode);
+    _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", exitcode);
     log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0);
   }
 
@@ -551,8 +552,8 @@ void throttle_restart() {
   if (throttle > 7) throttle = 8;
 
   char threshold[8], milliseconds[8];
-  _snprintf(threshold, sizeof(threshold), "%d", throttle_delay);
-  _snprintf(milliseconds, sizeof(milliseconds), "%d", ms);
+  _snprintf_s(threshold, sizeof(threshold), _TRUNCATE, "%d", throttle_delay);
+  _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%d", ms);
   log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0);
 
   if (throttle_timer) {