Skip to content

Commit c661341

Browse files
mbanckCommitfest Bot
authored andcommitted
Make safeguard against incorrect fd flags for fsync more portable.
The current code assumed that O_RDONLY is defined as 0, but this is not universally the case. To fix this, mask the flags with O_ACCMODE and assert that they are O_RDONLY for directories or not O_RDONLY for files. Co-authored-by: Samuel Thibault
1 parent f20a347 commit c661341

File tree

1 file changed

+4
-6
lines changed
  • src/backend/storage/file

1 file changed

+4
-6
lines changed

src/backend/storage/file/fd.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,12 @@ pg_fsync(int fd)
411411
{
412412
int desc_flags = fcntl(fd, F_GETFL);
413413

414-
/*
415-
* O_RDONLY is historically 0, so just make sure that for directories
416-
* no write flags are used.
417-
*/
414+
desc_flags &= O_ACCMODE;
415+
418416
if (S_ISDIR(st.st_mode))
419-
Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0);
417+
Assert(desc_flags == O_RDONLY);
420418
else
421-
Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0);
419+
Assert(desc_flags != O_RDONLY);
422420
}
423421
errno = 0;
424422
#endif

0 commit comments

Comments
 (0)