From a334319f6530564d22e775935d9c91663623a1b4 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Wed, 22 Dec 2004 20:10:10 +0000 Subject: (CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4. --- nis/nis_subr.c | 105 +++++++++++++++++++-------------------------------------- 1 file changed, 35 insertions(+), 70 deletions(-) (limited to 'nis/nis_subr.c') diff --git a/nis/nis_subr.c b/nis/nis_subr.c index c68189e541..4c4ef8b5b4 100644 --- a/nis/nis_subr.c +++ b/nis/nis_subr.c @@ -1,4 +1,4 @@ -/* Copyright (c) 1997,1999,2000,2004,2005,2006 Free Software Foundation, Inc. +/* Copyright (c) 1997, 1999, 2000, 2004 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Thorsten Kukuk , 1997. @@ -39,13 +39,22 @@ nis_leaf_of_r (const_nis_name name, char *buffer, size_t buflen) while (name[i] != '.' && name[i] != '\0') i++; - if (__builtin_expect (i >= buflen, 0)) + if (i > buflen - 1) { __set_errno (ERANGE); return NULL; } - *((char *) __mempcpy (buffer, name, i)) = '\0'; + if (i > 0) + { + if ((size_t)i >= buflen) + { + __set_errno (ERANGE); + return NULL; + } + + *((char *) __mempcpy (buffer, name, i)) = '\0'; + } return buffer; } @@ -89,12 +98,13 @@ nis_name_of_r (const_nis_name name, char *buffer, size_t buflen) } libnsl_hidden_def (nis_name_of_r) -static int __always_inline +static int count_dots (const_nis_name str) { int count = 0; + size_t l = strlen (str); - for (size_t i = 0; str[i] != '\0'; ++i) + for (size_t i = 0; i < l; ++i) if (str[i] == '.') ++count; @@ -107,23 +117,25 @@ count_dots (const_nis_name str) nis_name * nis_getnames (const_nis_name name) { - const char *local_domain = nis_local_directory (); - size_t local_domain_len = strlen (local_domain); - size_t name_len = strlen (name); + nis_name *getnames = NULL; + char local_domain[NIS_MAXNAMELEN + 1]; char *path; + char *cp; + int count; int pos = 0; - char *saveptr; int have_point; - const char *cp; - const char *cp2; + char *saveptr; - int count = 2; - nis_name *getnames = malloc ((count + 1) * sizeof (char *)); + strncpy (local_domain, nis_local_directory (), NIS_MAXNAMELEN); + local_domain[NIS_MAXNAMELEN] = '\0'; + + count = 1; + getnames = malloc ((count + 1) * sizeof (char *)); if (__builtin_expect (getnames == NULL, 0)) return NULL; /* Do we have a fully qualified NIS+ name ? If yes, give it back */ - if (name[name_len - 1] == '.') + if (name[strlen (name) - 1] == '.') { if ((getnames[0] = strdup (name)) == NULL) { @@ -139,44 +151,6 @@ nis_getnames (const_nis_name name) return getnames; } - /* If the passed NAME is shared a suffix (the latter of course with - a final dot) with each other we pass back NAME with a final - dot. */ - if (local_domain_len > 2) - { - have_point = 0; - cp = &local_domain[local_domain_len - 2]; - cp2 = &name[name_len - 1]; - - while (*cp == *cp2) - { - if (*cp == '.') - have_point = 1; - --cp; - --cp2; - if (cp < local_domain) - { - have_point = cp2 < name || *cp2 == '.'; - break; - } - if (cp2 < name) - { - have_point = *cp == '.'; - break; - } - } - - if (have_point) - { - getnames[0] = malloc (name_len + 2); - if (getnames[0] == NULL) - goto free_null; - - strcpy (stpcpy (getnames[0], name), "."); - ++pos; - } - } - /* Get the search path, where we have to search "name" */ path = getenv ("NIS_PATH"); if (path == NULL) @@ -184,17 +158,17 @@ nis_getnames (const_nis_name name) else path = strdupa (path); - have_point = strchr (name, '.') != NULL; + have_point = (strchr (name, '.') != NULL); cp = __strtok_r (path, ":", &saveptr); while (cp) { if (strcmp (cp, "$") == 0) { - const char *cptr = local_domain; + char *cptr = local_domain; char *tmp; - while (*cptr != '\0' && count_dots (cptr) >= 2) + while ((have_point && *cptr != '\0') || (count_dots (cptr) >= 2)) { if (pos >= count) { @@ -205,7 +179,8 @@ nis_getnames (const_nis_name name) goto free_null; getnames = newp; } - tmp = malloc (strlen (cptr) + local_domain_len + name_len + 2); + tmp = malloc (strlen (cptr) + strlen (local_domain) + + strlen (name) + 2); if (__builtin_expect (tmp == NULL, 0)) goto free_null; @@ -235,7 +210,7 @@ nis_getnames (const_nis_name name) { char *p; - tmp = malloc (cplen + local_domain_len + name_len + 2); + tmp = malloc (cplen + strlen (local_domain) + strlen (name) + 2); if (__builtin_expect (tmp == NULL, 0)) goto free_null; @@ -251,16 +226,13 @@ nis_getnames (const_nis_name name) { char *p; - tmp = malloc (cplen + name_len + 3); + tmp = malloc (cplen + strlen (name) + 2); if (__builtin_expect (tmp == NULL, 0)) goto free_null; - p = __mempcpy (tmp, name, name_len); + p = __stpcpy (tmp, name); *p++ = '.'; - p = __mempcpy (p, cp, cplen); - if (p[-1] != '.') - *p++ = '.'; - *p = '\0'; + memcpy (p, cp, cplen + 1); } if (pos >= count) @@ -278,13 +250,6 @@ nis_getnames (const_nis_name name) cp = __strtok_r (NULL, ":", &saveptr); } - if (pos == 0 - && __asprintf (&getnames[pos++], "%s%s%s%s", - name, name[name_len - 1] == '.' ? "" : ".", - local_domain, - local_domain[local_domain_len - 1] == '.' ? "" : ".") < 0) - goto free_null; - getnames[pos] = NULL; return getnames; -- cgit v1.2.3