exec_invalid_args.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. int main(int argc, const char** argv, const char** envp) {
  6. int r;
  7. char** badptr_argv = (char**)-1;
  8. char* badptr = (char*)-1;
  9. char* bad_argv[] = {badptr, NULL};
  10. char* good_argv[] = {"DUMMY", NULL};
  11. char* bad_envp[] = {badptr, NULL};
  12. char* good_envp[] = {"DUMMY", NULL};
  13. r = execve(badptr, good_argv, good_envp);
  14. if (r == -1 && errno == EFAULT)
  15. printf("execve(invalid-path) correctly returned error\n");
  16. r = execve(argv[0], badptr_argv, good_envp);
  17. if (r == -1 && errno == EFAULT)
  18. printf("execve(invalid-argv-ptr) correctly returned error\n");
  19. r = execve(argv[0], good_argv, badptr_argv);
  20. if (r == -1 && errno == EFAULT)
  21. printf("execve(invalid-envp-ptr) correctly returned error\n");
  22. r = execve(argv[0], bad_argv, good_envp);
  23. if (r == -1 && errno == EFAULT)
  24. printf("execve(invalid-argv) correctly returned error\n");
  25. r = execve(argv[0], good_argv, bad_envp);
  26. if (r == -1 && errno == EFAULT)
  27. printf("execve(invalid-envp) correctly returned error\n");
  28. return 0;
  29. }