Added check for failed memory allocation. Set binary to output to the
[ovpnpermit.git] / main.c
diff --git a/main.c b/main.c
index 0a133b2..cce28db 100644 (file)
--- a/main.c
+++ b/main.c
@@ -13,23 +13,29 @@ int main (int argc, char *argv[]) {
     char ** newargv = malloc (sizeof(char*) * (argc + 1));
     int arg;
 
-    // Transfer the arguments passed in to a new argument array
-    for (arg = 0; arg < argc; arg++) {
-        newargv[arg] = argv[arg];
-    }
-    // Ensure the argument list is NULL terminated (for execvp)
-    newargv[argc] = NULL;
-    // Ensure the first argument matches the command
-    newargv[0] = command;
+    if (newargv != NULL) {
+        // Transfer the arguments passed in to a new argument array
+        for (arg = 0; arg < argc; arg++) {
+            newargv[arg] = argv[arg];
+        }
+        // Ensure the argument list is NULL terminated (for execvp)
+        newargv[argc] = NULL;
+        // Ensure the first argument matches the command
+        newargv[0] = command;
 
-    // Execute the external command
-    //setuid (0);
-    execvp (command, newargv);
+        // Execute the external command
+        //setuid (0);
+        execvp (command, newargv);
 
-    // This part of the code shouldn't be reached
-    printf ("Error executing openvpn\n");
+        // This part of the code shouldn't be reached unless there was a problem
+        fprintf (stderr, "Error executing openvpn\n");
 
-    free (newargv);
+        free (newargv);
+    }
+    else {
+        // There was a problem with thee malloc call
+        fprintf (stderr, "Error allocating memory for command argument list\n");
+    }
     return 0;
 }