diff --git a/main.cpp b/main.cpp index 152b0e61..50de5e1d 100644 --- a/main.cpp +++ b/main.cpp @@ -1198,6 +1198,16 @@ int read_input(std::vector &sources, char *fname, int maxzoom, int minzo double dist_sum = 0; size_t dist_count = 0; + int files_open_before_reading = open("/dev/null", O_RDONLY | O_CLOEXEC); + if (files_open_before_reading < 0) { + perror("open /dev/null"); + exit(EXIT_FAILURE); + } + if (close(files_open_before_reading) != 0) { + perror("close"); + exit(EXIT_FAILURE); + } + size_t nsources = sources.size(); for (size_t source = 0; source < nsources; source++) { std::string reading; @@ -1383,6 +1393,11 @@ int read_input(std::vector &sources, char *fname, int maxzoom, int minzo perror("munmap source file"); exit(EXIT_FAILURE); } + + if (close(fd) != 0) { + perror("close input file"); + exit(EXIT_FAILURE); + } } else { FILE *fp = fdopen(fd, "r"); if (fp == NULL) { @@ -1543,6 +1558,22 @@ int read_input(std::vector &sources, char *fname, int maxzoom, int minzo } } + int files_open_after_reading = open("/dev/null", O_RDONLY | O_CLOEXEC); + if (files_open_after_reading < 0) { + perror("open /dev/null"); + exit(EXIT_FAILURE); + } + if (close(files_open_after_reading) != 0) { + perror("close"); + exit(EXIT_FAILURE); + } + + if (files_open_after_reading > files_open_before_reading) { + fprintf(stderr, "Internal error: Files left open after reading input. (%d vs %d)\n", + files_open_before_reading, files_open_after_reading); + ret = EXIT_FAILURE; + } + if (!quiet) { fprintf(stderr, " \r"); // (stderr, "Read 10000.00 million features\r", *progress_seq / 1000000.0);