Tea Inside Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Alviro Iskandar Setiawan <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Alviro Iskandar Setiawan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>,
	Tea Inside Mailing List <[email protected]>,
	Ammar Faizi <[email protected]>,
	Louvian Lyndal <[email protected]>,
	Michael Arminto <[email protected]>
Subject: [PATCH teavpn2 3/3] arch/linux: syscall: Fix retval checking in libc syscall
Date: Fri, 27 May 2022 00:02:27 +0000	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

The libc syscall wrappers mostly return -1 when they fail, then they
set the error code to the @errno variable. The current code seems to
be doing something wrong. We assume it errors when the return value
is negative. However, not all negative values are meant to be an
error indicator. On Linux, the only reserved error code is within
range [-4095, -1]. That means we still have a potential to get a
negative return value that is not an error.

I understand that most of them work fine here because of the nature
of the syscall itself that won't return a negative value upen
succcessful. But the above assumption about the negative value is
not correct.

Replace the error checking from (ret < 0) to (ret == -1) to reflect
the above fact.

Fixes: ca111cce3c05aed1d3a078c095c0111f3c48484f ("arch: Add generic arch syscalls from libc")
Cc: Ammar Faizi <[email protected]>
Cc: Louvian Lyndal <[email protected]>
Cc: Michael Arminto <[email protected]>
Signed-off-by: Alviro Iskandar Setiawan <[email protected]>
---
 src/teavpn2/arch/generic/linux.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/teavpn2/arch/generic/linux.h b/src/teavpn2/arch/generic/linux.h
index 42e7201..0dc12e8 100644
--- a/src/teavpn2/arch/generic/linux.h
+++ b/src/teavpn2/arch/generic/linux.h
@@ -20,21 +20,21 @@ static inline int __sys_epoll_wait(int epfd, struct epoll_event *events,
 {
 	int ret;
 	ret = epoll_wait(epfd, events, maxevents, timeout);
-	return unlikely(ret < 0) ? -errno : ret;
+	return unlikely(ret == -1) ? -errno : ret;
 }
 
 static inline ssize_t __sys_read(int fd, void *buf, size_t len)
 {
 	ssize_t ret;
 	ret = read(fd, buf, len);
-	return unlikely(ret < 0) ? (ssize_t) -errno : ret;
+	return unlikely(ret == -1) ? (ssize_t) -errno : ret;
 }
 
 static inline ssize_t __sys_write(int fd, const void *buf, size_t len)
 {
 	ssize_t ret;
 	ret = write(fd, buf, len);
-	return unlikely(ret < 0) ? (ssize_t) -errno : ret;
+	return unlikely(ret == -1) ? (ssize_t) -errno : ret;
 }
 
 static inline ssize_t __sys_recvfrom(int sockfd, void *buf, size_t len,
@@ -43,7 +43,7 @@ static inline ssize_t __sys_recvfrom(int sockfd, void *buf, size_t len,
 {
 	ssize_t ret;
 	ret = recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
-	return unlikely(ret < 0) ? (ssize_t) -errno : ret;
+	return unlikely(ret == -1) ? (ssize_t) -errno : ret;
 }
 
 static inline ssize_t __sys_sendto(int sockfd, const void *buf, size_t len,
@@ -52,14 +52,14 @@ static inline ssize_t __sys_sendto(int sockfd, const void *buf, size_t len,
 {
 	ssize_t ret;
 	ret = sendto(sockfd, buf, len, flags, dest_addr, addrlen);
-	return unlikely(ret < 0) ? (ssize_t) -errno : ret;
+	return unlikely(ret == -1) ? (ssize_t) -errno : ret;
 }
 
 static inline int __sys_close(int fd)
 {
 	int ret;
 	ret = close(fd);
-	return unlikely(ret < 0) ? -errno : ret;
+	return unlikely(ret == -1) ? -errno : ret;
 }
 
 #endif /* #ifndef TEAVPN2__ARCH__GENERIC__LINUX_H */
-- 
Alviro Iskandar Setiawan


  parent reply	other threads:[~2022-05-27  0:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-27  0:02 [PATCH teavpn2 0/3] teavpn2 fixes Alviro Iskandar Setiawan
2022-05-27  0:02 ` [PATCH teavpn2 1/3] allocator: Fix `@errno` value when overflow Alviro Iskandar Setiawan
2022-05-27  0:02 ` [PATCH teavpn2 2/3] net: iface: Fix a potential NULL pointer dereference Alviro Iskandar Setiawan
2022-05-27  0:02 ` Alviro Iskandar Setiawan [this message]
2022-05-27  0:14 ` [PATCH teavpn2 0/3] teavpn2 fixes Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220527000227.1253934-4-alviro.iskandar@gnuweeb.org \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox