From cb6af3a04cc00cfa2af9018b535169d7c75ab2d1 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Mon, 25 Feb 2019 20:55:03 +0200 Subject: [PATCH] fs-posix: fs_read_stream() - Don't close file's fd This is especially important with newly created files, because they may still be accessed after reading. The next file access attempt after fs_read_stream() might cause the file to be recreated and crash. Fixes: Panic: file fs-posix.c: line 252 (fs_posix_create): assertion failed: (file->temp_path == NULL) --- src/lib-fs/fs-posix.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib-fs/fs-posix.c b/src/lib-fs/fs-posix.c index 20531db156..194fdec1e5 100644 --- a/src/lib-fs/fs-posix.c +++ b/src/lib-fs/fs-posix.c @@ -444,12 +444,18 @@ fs_posix_read_stream(struct fs_file *_file, size_t max_buffer_size) { struct posix_fs_file *file = (struct posix_fs_file *)_file; struct istream *input; + int fd_dup; if (fs_posix_open_for_read(file) < 0) input = i_stream_create_error_str(errno, "%s", fs_last_error(_file->fs)); + else if ((fd_dup = dup(file->fd)) == -1) + input = i_stream_create_error_str(errno, "dup() failed: %m"); else { - /* the stream could live even after the fs_file */ - input = i_stream_create_fd_autoclose(&file->fd, max_buffer_size); + /* The stream could live even after the fs_file. + Don't use file->fd directly, because the fd may still be + used for other purposes. It's especially important for files + that were just created. */ + input = i_stream_create_fd_autoclose(&fd_dup, max_buffer_size); } i_stream_set_name(input, file->full_path); return input; -- 2.47.3