From 9727e8ada22d4da8a492b7da0a19d88e4394d59e Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 24 Oct 2012 21:51:16 -0700 Subject: [PATCH] Overload expand_parameter() like get_number(). Allow expand_parameter() to accept the optional must_exist flag. --- registry.cpp | 18 ++++++++++++++---- registry.h | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/registry.cpp b/registry.cpp index 54a6114..d6cde23 100644 --- a/registry.cpp +++ b/registry.cpp @@ -141,28 +141,34 @@ int set_environment(char *service_name, HKEY key, char **env) { return 0; } -int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise) { +int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise, bool must_exist) { unsigned char *buffer = (unsigned char *) HeapAlloc(GetProcessHeap(), 0, datalen); if (! buffer) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, "expand_parameter()", 0); return 1; } + ZeroMemory(data, datalen); + unsigned long type = REG_EXPAND_SZ; unsigned long buflen = datalen; unsigned long ret = RegQueryValueEx(key, value, 0, &type, buffer, &buflen); if (ret != ERROR_SUCCESS) { - if (ret != ERROR_FILE_NOT_FOUND) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0); + unsigned long error = GetLastError(); HeapFree(GetProcessHeap(), 0, buffer); + + if (ret == ERROR_FILE_NOT_FOUND) { + if (! must_exist) return 0; + } + + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(error), 0); return 2; } /* Paths aren't allowed to contain quotes. */ if (sanitise) PathUnquoteSpaces((LPSTR) buffer); - ZeroMemory(data, datalen); - /* Technically we shouldn't expand environment strings from REG_SZ values */ if (type != REG_EXPAND_SZ) { memmove(data, buffer, buflen); @@ -181,6 +187,10 @@ int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, b return 0; } +int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise) { + return expand_parameter(key, value, data, datalen, sanitise, true); +} + /* Query an unsigned long from the registry. Returns: 1 if a number was retrieved. diff --git a/registry.h b/registry.h index 561318e..6344313 100644 --- a/registry.h +++ b/registry.h @@ -13,6 +13,7 @@ int create_messages(); int create_parameters(char *, char *, char *, char *); int create_exit_action(char *, const char *); int set_environment(char *, HKEY, char **); +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 *); -- 2.7.4