Skip to content

Commit 6a375cb

Browse files
committed
core-shim: fix incorrect errno and return when using posix_madvise
posix_madvise returns a +ve errno value unlike madvise; fix the current implementation to return -1 and set errno as per the madvise semantics. Kudos to Christian Franke for reporting this issue with a suitable suggested fix. Closes: #636 Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
1 parent 165c6c9 commit 6a375cb

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

core-shim.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ int shim_madvise(void *addr, size_t length, int advice)
11631163
return (int)syscall(__NR_madvise, addr, length, advice);
11641164
#elif defined(HAVE_POSIX_MADVISE)
11651165
int posix_advice;
1166+
int ret;
11661167

11671168
switch (advice) {
11681169
#if defined(POSIX_MADV_NORMAL) && \
@@ -1199,7 +1200,13 @@ int shim_madvise(void *addr, size_t length, int advice)
11991200
posix_advice = POSIX_MADV_NORMAL;
12001201
break;
12011202
}
1202-
return (int)posix_madvise(addr, length, posix_advice);
1203+
/* unlike madvise, posix_madvise returns a +ve error code */
1204+
ret = posix_madvise(addr, length, posix_advice);
1205+
if (ret) {
1206+
errno = (ret > 0) ? ret : ENOSYS;
1207+
return -1;
1208+
}
1209+
return 0;
12031210
#else
12041211
return (int)shim_enosys(0, addr, length, advice);
12051212
#endif

0 commit comments

Comments
 (0)