* lib/safe-read.c (safe_rw) [__CYGWIN__]:
Work around bug in Cygwin up to at least 3.6.12, where
‘read’ and ‘write’ sizes lose high-order bits.  See:
https://cygwin.com/pipermail/cygwin/2026-November/260246.html
---
 ChangeLog        | 8 ++++++++
 lib/safe-read.c  | 9 +++++++++
 lib/sys-limits.h | 3 +++
 3 files changed, 20 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 951a3e419f..ee17ee5bec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2026-11-19  Paul Eggert  <[email protected]>
+
+       safe-read: work around Cygwin big-read bug
+       * lib/safe-read.c (safe_rw) [__CYGWIN__]:
+       Work around bug in Cygwin up to at least 3.6.12, where
+       ‘read’ and ‘write’ sizes lose high-order bits.  See:
+       https://cygwin.com/pipermail/cygwin/2026-November/260246.html
+
 2026-11-18  Bruno Haible  <[email protected]>
 
        host-cpu-c-abi: Add support for MSVC on Windows/arm64.
diff --git a/lib/safe-read.c b/lib/safe-read.c
index adf7168d10..5158029c07 100644
--- a/lib/safe-read.c
+++ b/lib/safe-read.c
@@ -55,6 +55,15 @@ typedef void *bufptr;
 ptrdiff_t
 safe_rw (int fd, bufptr buf, idx_t count)
 {
+#ifdef __CYGWIN__
+  /* Cygwin up to at least 3.6.12 silently discards all but the bottom
+     32 bits of COUNT.  This can falsely signal EOF on read and can
+     also make trouble for callers on write.  There are likely other
+     problems with some COUNT values greater than SYS_BUFSIZE_MAX.  */
+  if (SYS_BUFSIZE_MAX < count)
+    count = SYS_BUFSIZE_MAX;
+#endif
+
   for (;;)
     {
       ssize_t result = rw (fd, buf, count <= (size_t) -1 ? count : (size_t) 
-1);
diff --git a/lib/sys-limits.h b/lib/sys-limits.h
index d61ebdf79d..728d42a30d 100644
--- a/lib/sys-limits.h
+++ b/lib/sys-limits.h
@@ -30,6 +30,9 @@
    Using this also works around a serious Linux bug before 2.6.16; see
    <https://bugzilla.redhat.com/show_bug.cgi?id=612839>.
 
+   It also works around a similar bug in Cygwin to at least 3.6.12; see
+   <https://cygwin.com/pipermail/cygwin/2026-November/260246.html>.
+
    Using this is likely to work around similar bugs in other operating
    systems.  */
 
-- 
2.56.1


Reply via email to