Hi Michal,

On 31/5/24 09:28, Michal Privoznik wrote:
The unspoken premise of qemu_madvise() is that errno is set on
error. And it is mostly the case except for posix_madvise() which
is documented to return either zero (on success) or a positive
error number.

Watch out, Linux:

  RETURN VALUE

     On success, posix_madvise() returns 0.  On failure,
     it returns a positive error number.

but on Darwin:

  RETURN VALUES

     Upon successful completion, a value of 0 is returned.
     Otherwise, a value of -1 is returned and errno is set
     to indicate the error.

(Haven't checked other POSIX OSes).

So we likely need more #ifdef'ry here.

This means, we must set errno ourselves. And while
at it, make the function return a negative value on error, just
like other error paths do.

Signed-off-by: Michal Privoznik <mpriv...@redhat.com>
Reviewed-by: David Hildenbrand <da...@redhat.com>
---
  util/osdep.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/util/osdep.c b/util/osdep.c
index e996c4744a..e42f4e8121 100644
--- a/util/osdep.c
+++ b/util/osdep.c
@@ -57,7 +57,12 @@ int qemu_madvise(void *addr, size_t len, int advice)
  #if defined(CONFIG_MADVISE)
      return madvise(addr, len, advice);
  #elif defined(CONFIG_POSIX_MADVISE)
-    return posix_madvise(addr, len, advice);
+    int rc = posix_madvise(addr, len, advice);
+    if (rc) {
+        errno = rc;
+        return -1;
+    }
+    return 0;
  #else
      errno = EINVAL;
      return -1;


Reply via email to