]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nscd/nscd_getserv_r.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nscd / nscd_getserv_r.c
index de96a5757b86c27273a1991663a643787f84acb1..2c0efbd8101b39e66addcc2d4b814313431c137c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2007, 2009, 2011 Free Software Foundation, Inc.
+/* Copyright (C) 2007-2020 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@redhat.com>, 2007.
 
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
 #include <errno.h>
 #include <string.h>
 #include <not-cancel.h>
-#include <stdio-common/_itoa.h>
+#include <_itoa.h>
+#include <stdint.h>
 
 #include "nscd-client.h"
 #include "nscd_proto.h"
@@ -54,7 +54,7 @@ __nscd_getservbyport_r (int port, const char *proto,
   portstr[sizeof (portstr) - 1] = '\0';
   char *cp = _itoa_word (port, portstr + sizeof (portstr) - 1, 10, 0);
 
-  return nscd_getserv_r (cp, portstr + sizeof (portstr) - cp, proto,
+  return nscd_getserv_r (cp, portstr + sizeof (portstr) - 1 - cp, proto,
                         GETSERVBYPORT, result_buf, buf, buflen, result);
 }
 
@@ -124,6 +124,7 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
          s_name = (char *) (&found->data[0].servdata + 1);
          serv_resp = found->data[0].servdata;
          s_proto = s_name + serv_resp.s_name_len;
+         alloca_aliases_len = 1;
          aliases_len = (uint32_t *) (s_proto + serv_resp.s_proto_len);
          aliases_list = ((char *) aliases_len
                          + serv_resp.s_aliases_cnt * sizeof (uint32_t));
@@ -140,7 +141,7 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
                                > recend, 0))
            goto out;
 
-#ifndef _STRING_ARCH_unaligned
+#if !_STRING_ARCH_unaligned
          /* The aliases_len array in the mapped database might very
             well be unaligned.  We will access it word-wise so on
             platforms which do not tolerate unaligned accesses we
@@ -154,7 +155,9 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
                                     + (serv_resp.s_aliases_cnt
                                        * sizeof (uint32_t)));
              if (alloca_aliases_len)
-               tmp = __alloca (serv_resp.s_aliases_cnt * sizeof (uint32_t));
+               tmp = alloca_account (serv_resp.s_aliases_cnt
+                                     * sizeof (uint32_t),
+                                     alloca_used);
              else
                {
                  tmp = malloc (serv_resp.s_aliases_cnt * sizeof (uint32_t));
@@ -186,7 +189,7 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
   /* No value found so far.  */
   *result = NULL;
 
-  if (__builtin_expect (serv_resp.found == -1, 0))
+  if (__glibc_unlikely (serv_resp.found == -1))
     {
       /* The daemon does not cache this database.  */
       __nss_not_use_nscd_services = 1;
@@ -249,8 +252,9 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
                                     + (serv_resp.s_aliases_cnt
                                        * sizeof (uint32_t)));
              if (alloca_aliases_len)
-               aliases_len = alloca (serv_resp.s_aliases_cnt
-                                     * sizeof (uint32_t));
+               aliases_len = alloca_account (serv_resp.s_aliases_cnt
+                                             * sizeof (uint32_t),
+                                             alloca_used);
              else
                {
                  aliases_len = malloc (serv_resp.s_aliases_cnt
@@ -296,7 +300,7 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
        }
 
       /* See whether this would exceed the buffer capacity.  */
-      if (__builtin_expect (cp > buf + buflen, 0))
+      if (__glibc_unlikely (cp > buf + buflen))
        {
          /* aliases_len array might contain garbage during nscd GC cycle,
             retry rather than fail in that case.  */
@@ -352,7 +356,7 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
 
  out_close:
   if (sock != -1)
-    close_not_cancel_no_status (sock);
+    __close_nocancel_nostatus (sock);
  out:
   if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
     {
@@ -368,7 +372,11 @@ nscd_getserv_r (const char *crit, size_t critlen, const char *proto,
        }
 
       if (retval != -1)
-       goto retry;
+       {
+         if (!alloca_aliases_len)
+           free ((void *) aliases_len);
+         goto retry;
+       }
     }
 
   if (!alloca_aliases_len)