[PATCH v2] Cygwin: Fix error return for madvise()

Mark Geisert <[email protected]> Wed, 8 Jul 2026 01:03:05 -0700
Newsgroups gmane.os.cygwin.patches
Message-ID <[email protected]>
Currently madvise() and posix_madvise() are wired together as one
function: the latter.  But their error returns should be different.
Make madvise a first-class export in cygwin.din.

v2: Create madvise_worker() and have madvise() and posix_madvise()
    call it, then handling their error returns compliant to POSIX.
    Add a release note for 3.7.0.

Reported-by: Christian Franke <[email protected]>
Addresses: https://cygwin.com/pipermail/cygwin/2026-July/259872.html
Signed-off-by: Mark Geisert <[email protected]>
Fixes: 61522196c715 (* Merge in cygwin-64bit-branch.)
Reviewed-by: Takashi Yano, Christian Franke

---
 winsup/cygwin/cygwin.din    |  2 +-
 winsup/cygwin/mm/mmap.cc    | 24 ++++++++++++++++++++++--
 winsup/cygwin/release/3.7.0 |  8 ++++++++
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/cygwin.din b/winsup/cygwin/cygwin.din
index 2e53bc819..937eacdaf 100644
--- a/winsup/cygwin/cygwin.din
+++ b/winsup/cygwin/cygwin.din
@@ -951,7 +951,7 @@ lseek SIGFE
 lsetxattr SIGFE
 lstat SIGFE
 lutimes SIGFE
-madvise = posix_madvise SIGFE
+madvise SIGFE
 makecontext NOSIGFE
 mallinfo SIGFE
 malloc SIGFE
diff --git a/winsup/cygwin/mm/mmap.cc b/winsup/cygwin/mm/mmap.cc
index 1416e4ddc..bce819eb0 100644
--- a/winsup/cygwin/mm/mmap.cc
+++ b/winsup/cygwin/mm/mmap.cc
@@ -1422,8 +1422,8 @@ munlock (const void *addr, size_t len)
   return ret;
 }
 
-extern "C" int
-posix_madvise (void *addr, size_t len, int advice)
+static int
+madvise_worker (void *addr, size_t len, int advice)
 {
   int ret = 0;
   /* Check parameters. */
@@ -1514,6 +1514,26 @@ posix_madvise (void *addr, size_t len, int advice)
       break;
     }
 out:
+  return ret;
+}
+
+extern "C" int
+madvise (void *addr, size_t len, int advice)
+{
+  int ret = madvise_worker (addr, len, advice);
+  if (ret > 0)
+    {
+      set_errno (ret);
+      ret = -1;
+    }
+  syscall_printf ("%R = madvise(%p, %lu, %d)", ret, addr, len, advice);
+  return ret;
+}
+
+extern "C" int
+posix_madvise (void *addr, size_t len, int advice)
+{
+  int ret = madvise_worker (addr, len, advice);
   syscall_printf ("%d = posix_madvise(%p, %lu, %d)", ret, addr, len, advice);
   return ret;
 }
diff --git a/winsup/cygwin/release/3.7.0 b/winsup/cygwin/release/3.7.0
index 3fc32433e..3f6a0ecd7 100644
--- a/winsup/cygwin/release/3.7.0
+++ b/winsup/cygwin/release/3.7.0
@@ -25,3 +25,11 @@ What's new:
 - Now, a Cygwin process started from a non‑Cygwin process on a pseudo console
   runs on a pty rather than on the console device originating from the pseudo
   console.
+
+Fixes:
+------
+
+- Error return logic for madvise() is now separated from posix_madvise().
+  If madvise() errors, it returns -1 with errno set. If posix_madvise()
+  errors, it returns an error number without changing errno.
+  Addresses: https://cygwin.com/pipermail/cygwin/2026-July/259872.html
-- 
2.51.0