fdleak.c 634 B

123456789101112131415161718192021222324252627
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. /* This is supposed to expose resource leaks where close()d files are not properly cleaned up. */
  8. int main(int argc, char** argv) {
  9. for (int i = 0; i < 10000; i++) {
  10. int fd = open(argv[0], O_RDONLY);
  11. if (fd == -1)
  12. abort();
  13. char buf[1024];
  14. ssize_t read_ret = read(fd, buf, sizeof(buf));
  15. if (read_ret == -1)
  16. abort();
  17. int ret = close(fd);
  18. if (ret == -1)
  19. abort();
  20. }
  21. puts("Test succeeded.");
  22. return 0;
  23. }