stat_invalid_args.c 943 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. int main(int argc, char** argv) {
  8. int r;
  9. struct stat buf;
  10. char* goodpath = argv[0];
  11. char* badpath = (void*)-1;
  12. struct stat* goodbuf = &buf;
  13. struct stat* badbuf = (void*)-1;
  14. /* check stat() */
  15. r = stat(badpath, goodbuf);
  16. if (r == -1 && errno == EFAULT)
  17. printf("stat(invalid-path-ptr) correctly returned error\n");
  18. r = stat(goodpath, badbuf);
  19. if (r == -1 && errno == EFAULT)
  20. printf("stat(invalid-buf-ptr) correctly returned error\n");
  21. /* check lstat() */
  22. r = lstat(badpath, goodbuf);
  23. if (r == -1 && errno == EFAULT)
  24. printf("lstat(invalid-path-ptr) correctly returned error\n");
  25. r = lstat(goodpath, badbuf);
  26. if (r == -1 && errno == EFAULT)
  27. printf("lstat(invalid-buf-ptr) correctly returned error\n");
  28. return 0;
  29. }