From 92091a697ae8dac4026fd75a421ad9464aaa253e Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Wed, 25 Oct 2023 15:44:19 +0800
Subject: [PATCH 76/77] fix recvmsg return EINVAL

---
 src/api/sockets.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/api/sockets.c b/src/api/sockets.c
index b6c7b05..1d71427 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -1492,9 +1492,21 @@ lwip_recvmsg(int s, struct msghdr *message, int flags)
   /* check for valid vectors */
   buflen = 0;
   for (i = 0; i < message->msg_iovlen; i++) {
+#if GAZELLE_ENABLE
+    /* msg_iov[i].iov_len == 0 dont return ERRVAL
+     * According to the Single Unix Specification we should return EINVAL if an elment length is < 0
+     * when cast to ssize_t
+     */
+    if ((message->msg_iov[i].iov_base == NULL) || ((ssize_t)message->msg_iov[i].iov_len < 0) ||
+#else
     if ((message->msg_iov[i].iov_base == NULL) || ((ssize_t)message->msg_iov[i].iov_len <= 0) ||
+#endif
         ((size_t)(ssize_t)message->msg_iov[i].iov_len != message->msg_iov[i].iov_len) ||
+#if GAZELLE_ENABLE
+        ((ssize_t)(buflen + (ssize_t)message->msg_iov[i].iov_len) < 0)) {
+#else
         ((ssize_t)(buflen + (ssize_t)message->msg_iov[i].iov_len) <= 0)) {
+#endif
       sock_set_errno(sock, err_to_errno(ERR_VAL));
       done_socket(sock);
       return -1;
-- 
2.27.0

