Browse Source

Overload expand_parameter() like get_number().

Allow expand_parameter() to accept the optional must_exist flag.
Iain Patterson 9 years ago
parent
commit
9727e8ada2
2 changed files with 15 additions and 4 deletions
  1. 14 4
      registry.cpp
  2. 1 0
      registry.h

+ 14 - 4
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.

+ 1 - 0
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 *);