stat_invalid_args.c 969 B

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