From c66134119fb2fb7a1446c12196004ba5fab65b88 Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Tue, 10 Jun 2025 12:23:17 +0200 Subject: [PATCH 1/2] 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 --- src/backend/storage/file/fd.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 0e8299dd5564..cd1e661974aa 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -411,14 +411,12 @@ pg_fsync(int fd) { int desc_flags = fcntl(fd, F_GETFL); - /* - * O_RDONLY is historically 0, so just make sure that for directories - * no write flags are used. - */ + desc_flags &= O_ACCMODE; + if (S_ISDIR(st.st_mode)) - Assert((desc_flags & (O_RDWR | O_WRONLY)) == 0); + Assert(desc_flags == O_RDONLY); else - Assert((desc_flags & (O_RDWR | O_WRONLY)) != 0); + Assert(desc_flags != O_RDONLY); } errno = 0; #endif From f77645846fd71eb522c149054b4496cb85b8ea4e Mon Sep 17 00:00:00 2001 From: Michael Banck Date: Tue, 10 Jun 2025 23:34:14 +0200 Subject: [PATCH 2/2] Make sure IOV_MAX is defined. We stopped defining IOV_MAX on non-Windows systems since 75357ab9 under the assumption that every non-Windows system defines it in limits.h already. However, the GNU (Hurd) system does not define this limit. As POSIX does not mandate IOV_MAX be defined, define it to 16 if it is undefined. Co-authored-by: Christoph Berg --- src/include/port/pg_iovec.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/include/port/pg_iovec.h b/src/include/port/pg_iovec.h index df40c7208be4..d29721bc7e45 100644 --- a/src/include/port/pg_iovec.h +++ b/src/include/port/pg_iovec.h @@ -21,9 +21,6 @@ #else -/* POSIX requires at least 16 as a maximum iovcnt. */ -#define IOV_MAX 16 - /* Define our own POSIX-compatible iovec struct. */ struct iovec { @@ -33,6 +30,11 @@ struct iovec #endif +/* POSIX requires at least 16 as a maximum iovcnt. */ +#ifndef IOV_MAX +#define IOV_MAX 16 +#endif + /* * Define a reasonable maximum that is safe to use on the stack in arrays of * struct iovec and other small types. The operating system could limit us to