huge_count_read_write.c 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author: SeongJae Park <sj@kernel.org>
  4. */
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #pragma GCC diagnostic push
  10. #if __GNUC__ >= 11 && __GNUC_MINOR__ >= 1
  11. /* Ignore read(2) overflow and write(2) overread compile warnings */
  12. #pragma GCC diagnostic ignored "-Wstringop-overread"
  13. #pragma GCC diagnostic ignored "-Wstringop-overflow"
  14. #endif
  15. void write_read_with_huge_count(char *file)
  16. {
  17. int filedesc = open(file, O_RDWR);
  18. char buf[25];
  19. int ret;
  20. printf("%s %s\n", __func__, file);
  21. if (filedesc < 0) {
  22. fprintf(stderr, "failed opening %s\n", file);
  23. exit(1);
  24. }
  25. write(filedesc, "", 0xfffffffful);
  26. perror("after write: ");
  27. ret = read(filedesc, buf, 0xfffffffful);
  28. perror("after read: ");
  29. close(filedesc);
  30. }
  31. #pragma GCC diagnostic pop
  32. int main(int argc, char *argv[])
  33. {
  34. if (argc != 2) {
  35. fprintf(stderr, "Usage: %s <file>\n", argv[0]);
  36. exit(1);
  37. }
  38. write_read_with_huge_count(argv[1]);
  39. return 0;
  40. }