fix potentially wrong-sign zero in cproj functions at infinity
[musl] / src / passwd / nscd_query.c
index f8d0fc1..dc3406b 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
 #include "nscd.h"
 
 static const struct {
@@ -22,7 +23,7 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *
        int32_t req_buf[REQ_LEN] = {
                NSCDVERSION,
                req,
-               strlen(key)+1
+               strnlen(key,LOGIN_NAME_MAX)+1
        };
        struct msghdr msg = {
                .msg_iov = (struct iovec[]){
@@ -31,24 +32,40 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *
                },
                .msg_iovlen = 2
        };
-
-       if (strlen(key) > INT32_MAX - 1) {
-               return (FILE*)-1;
-       }
+       int errno_save = errno;
 
        *swap = 0;
 retry:
+       memset(buf, 0, len);
+       buf[0] = NSCDVERSION;
 
        fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
-       if (fd < 0) return NULL;
+       if (fd < 0) {
+               if (errno == EAFNOSUPPORT) {
+                       f = fopen("/dev/null", "re");
+                       if (f)
+                               errno = errno_save;
+                       return f;
+               }
+               return 0;
+       }
+
+       if(!(f = fdopen(fd, "r"))) {
+               close(fd);
+               return 0;
+       }
+
+       if (req_buf[2] > LOGIN_NAME_MAX)
+               return f;
 
        if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
-               /* If there isn't a running nscd we return -1 to indicate that
-                * that is precisely what happened
-                */
+               /* If there isn't a running nscd we simulate a "not found"
+                * result and the caller is responsible for calling
+                * fclose on the (unconnected) socket. The value of
+                * errno must be left unchanged in this case.  */
                if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
-                       close(fd);
-                       return (FILE *)-1;
+                       errno = errno_save;
+                       return f;
                }
                goto error;
        }
@@ -56,8 +73,6 @@ retry:
        if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0)
                goto error;
 
-       if(!(f = fdopen(fd, "r"))) goto error;
-
        if (!fread(buf, len, 1, f)) {
                /* If the VERSION entry mismatches nscd will disconnect. The
                 * most likely cause is that the endianness mismatched. So, we
@@ -95,6 +110,6 @@ retry:
 
        return f;
 error:
-       if (f) fclose(f); else close(fd);
+       fclose(f);
        return 0;
 }