Skip to content

Commit 1e980ba

Browse files
committed
Remove resource leak in temp file creation
1 parent 3739d6b commit 1e980ba

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

include/osmium/index/detail/tmpfile.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ DEALINGS IN THE SOFTWARE.
3333
3434
*/
3535

36+
#include <osmium/io/detail/read_write.hpp>
37+
3638
#include <cerrno>
3739
#include <cstdio>
3840
#include <system_error>
@@ -43,16 +45,19 @@ namespace osmium {
4345

4446
/**
4547
* Create and open a temporary file. It is removed after opening.
48+
* After use close the file by calling close().
4649
*
4750
* @returns File descriptor of temporary file.
4851
* @throws std::system_error if something went wrong.
4952
*/
5053
inline int create_tmp_file() {
51-
FILE* file = ::tmpfile();
54+
FILE* file = std::tmpfile();
5255
if (!file) {
5356
throw std::system_error{errno, std::system_category(), "tempfile failed"};
5457
}
55-
return fileno(file);
58+
const int fd = osmium::io::detail::reliable_dup(fileno(file));
59+
std::fclose(file);
60+
return fd;
5661
}
5762

5863
} // namespace detail

0 commit comments

Comments
 (0)