fdleak.c 601 B

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