From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [138.197.159.143]) by gnuweeb.org (Postfix) with ESMTPSA id 1313D7E53F; Fri, 27 May 2022 00:02:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1653609761; bh=GqFfPvGW7zVuYt/x9HpoTCq24lWsTp+IS8s8VNQFDc0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eAHA9dcQdXOfPoRfhsNThQTl/Gbfpn/VVeroJTwzanqdnQHdjTB6nSr5t+z6MqCtD ygVaaqNmtrGCxdC4QtDb2pjDGZrfX63LwHPkepGIVG9J4FYJmLydisqWa2nRNUyVv+ CywcFOxMR8e616/8KJFYADFQybFlh9Uh2sr9P5+bGFHD6UWBhzXGbekbuoeHyw6Bp4 aXm9Tj2f3c+fwQmkIFNum6vUPJfFj7tA5HcVFpscQQppo7kEX/qHcJ0/U8Qse/NI0O b+Q+j4MuhCFFVqIFsWb3070pToVPsAuae3F2KvF+Gdd99SV2ssq7M/7qWhCSLGEWXk mOL18WI9+BnrQ== From: Alviro Iskandar Setiawan To: Ammar Faizi Cc: Alviro Iskandar Setiawan , GNU/Weeb Mailing List , Tea Inside Mailing List , Ammar Faizi , Louvian Lyndal , Michael Arminto Subject: [PATCH teavpn2 3/3] arch/linux: syscall: Fix retval checking in libc syscall Date: Fri, 27 May 2022 00:02:27 +0000 Message-Id: <20220527000227.1253934-4-alviro.iskandar@gnuweeb.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220527000227.1253934-1-alviro.iskandar@gnuweeb.org> References: <20220527000227.1253934-1-alviro.iskandar@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 Cc: Louvian Lyndal Cc: Michael Arminto Signed-off-by: Alviro Iskandar Setiawan --- 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