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;
}