I was just wondering if this is a good example, following good practices, of a C program for launching an interpreted script from a native binary executable. Here the interpreter is Perl and the script an implementation of something analogous to du --apparent-size, but the idea is general.Also, is this code secure against potentially hostile users, at least if Perl is the interpreter (for instance, in a suid or sgid program)?#include <unistd.h>#include <stdio.h>#include <string.h>#include <errno.h>int main(int argc, char** argv){  char* command[argc+3];  command[0] = /usr/bin/perl;  command[1] = -W;  command[2] = -T;  command[3] = /home/demetri/bin/awklikeperl.pl;  for (int i = 0; i < argc; i++) {    command[i+4] = argv[i+1];  }  execv(command[0], command);  int error = errno;  fprintf(stderr, Failed to exec: %s\n, strerror(error));  if (error == ENOENT) {    return(127);  } else {    return(126);  }}
Launching an interpreted script
c;security;perl;unix
No it is not safe:Classic off-by-one error. You reserve space for 3 additional entries with command[argc+3] but you add 4. argv[i+1] and command[i+4] will be out of bounds on the last iteration (last valid index is argv[argc-1] and command[argc+2] respectively)execv expects a NULL terminated array of NULL terminated strings. So the last entry in commands needs to be NULL (otherwise how would execv know how many arguments to pass when starting the process?).