[newlib-cygwin/main] Cygwin: Fix error return for madvise()

Christian Franke via Cygwin-cvs <[email protected]> Fri, 10 Jul 2026 15:24:17 +0000 (GMT)
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D383618c8902=
f5a14b258e1ffa3288a4e19780a76

commit 383618c8902f5a14b258e1ffa3288a4e19780a76
Author: Mark Geisert <[email protected]>
Date:   Wed Jul 8 01:03:00 2026 -0700

    Cygwin: Fix error return for madvise()
   =20
    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.
   =20
    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.
   =20
    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

Diff:
---
 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 =3D 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;
 }
=20
-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 =3D 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 =3D madvise_worker (addr, len, advice);
+  if (ret > 0)
+    {
+      set_errno (ret);
+      ret =3D -1;
+    }
+  syscall_printf ("%R =3D madvise(%p, %lu, %d)", ret, addr, len, advice);
+  return ret;
+}
+
+extern "C" int
+posix_madvise (void *addr, size_t len, int advice)
+{
+  int ret =3D madvise_worker (addr, len, advice);
   syscall_printf ("%d =3D posix_madvise(%p, %lu, %d)", ret, addr, len, adv=
ice);
   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=E2=80=91Cygwin process on a pse=
udo console
   runs on a pty rather than on the console device originating from the pse=
udo
   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