From: Iain Patterson Date: Thu, 31 Oct 2013 14:33:10 +0000 (+0000) Subject: Compiler food. X-Git-Tag: v2.17~4 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=ce9eb5d4646a0e279eae2909a18fccb1197e15fb;p=nssm.git Compiler food. Silence any compiler warnings relating to the use of _(v)snprintf() - not _(v)snprintf_s() - and type conversions. --- diff --git a/event.cpp b/event.cpp index baec6be..995b7af 100644 --- a/event.cpp +++ b/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); diff --git a/gui.cpp b/gui.cpp index 3d51236..84fd4c2 100644 --- a/gui.cpp +++ b/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]; diff --git a/io.cpp b/io.cpp index d692a4d..1c0a9ff 100644 --- a/io.cpp +++ b/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; } diff --git a/process.cpp b/process.cpp index e654811..4c00e64 100644 --- a/process.cpp +++ b/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. */ diff --git a/registry.cpp b/registry.cpp index aa16ac7..fbcc369 100644 --- a/registry.cpp +++ b/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); } diff --git a/registry.h b/registry.h index 2f45d81..6fd5d9c 100644 --- a/registry.h +++ b/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 diff --git a/service.cpp b/service.cpp index ac90ecb..f82b9bd 100644 --- a/service.cpp +++ b/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) {