mirror of
git://sourceware.org/git/glibc.git
synced 2025-03-06 20:58:33 +01:00
Replace rawmemchr (s, '\0') with strchr
Almost all uses of rawmemchr find the end of a string. Since most targets use a generic implementation, replacing it with strchr is better since that is optimized by compilers into strlen (s) + s. Also fix the generic rawmemchr implementation to use a cast to unsigned char in the if statement. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This commit is contained in:
parent
d2d3f3720c
commit
32c7acd464
24 changed files with 34 additions and 48 deletions
|
@ -30,7 +30,7 @@ typedef char *(*proto_t) (const char *, int);
|
||||||
char *
|
char *
|
||||||
generic_rawmemchr (const char *s, int c)
|
generic_rawmemchr (const char *s, int c)
|
||||||
{
|
{
|
||||||
if (c != 0)
|
if ((unsigned char) c != 0)
|
||||||
return memchr (s, c, PTRDIFF_MAX);
|
return memchr (s, c, PTRDIFF_MAX);
|
||||||
return (char *)s + strlen (s);
|
return (char *)s + strlen (s);
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ oldstrtok (char *s, const char *delim)
|
||||||
s = strpbrk (token, delim);
|
s = strpbrk (token, delim);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
/* This token finishes the string. */
|
/* This token finishes the string. */
|
||||||
olds = rawmemchr (token, '\0');
|
olds = strchr (token, '\0');
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Terminate the token and make OLDS point past it. */
|
/* Terminate the token and make OLDS point past it. */
|
||||||
|
|
|
@ -326,7 +326,7 @@ _dl_non_dynamic_init (void)
|
||||||
while (cp < unsecure_envvars + sizeof (unsecure_envvars))
|
while (cp < unsecure_envvars + sizeof (unsecure_envvars))
|
||||||
{
|
{
|
||||||
__unsetenv (cp);
|
__unsetenv (cp);
|
||||||
cp = (const char *) __rawmemchr (cp, '\0') + 1;
|
cp = strchr (cp, '\0') + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !HAVE_TUNABLES
|
#if !HAVE_TUNABLES
|
||||||
|
|
|
@ -1201,7 +1201,7 @@ main (int argc, char **argv)
|
||||||
if (opt_chroot != NULL)
|
if (opt_chroot != NULL)
|
||||||
{
|
{
|
||||||
/* Normalize the path a bit, we might need it for printing later. */
|
/* Normalize the path a bit, we might need it for printing later. */
|
||||||
char *endp = rawmemchr (opt_chroot, '\0');
|
char *endp = strchr (opt_chroot, '\0');
|
||||||
while (endp > opt_chroot && endp[-1] == '/')
|
while (endp > opt_chroot && endp[-1] == '/')
|
||||||
--endp;
|
--endp;
|
||||||
*endp = '\0';
|
*endp = '\0';
|
||||||
|
|
|
@ -1024,7 +1024,7 @@ ERROR: audit interface '%s' requires version %d (maximum supported version %d);
|
||||||
newp->fptr[cnt] = NULL;
|
newp->fptr[cnt] = NULL;
|
||||||
++cnt;
|
++cnt;
|
||||||
|
|
||||||
cp = rawmemchr (cp, '\0') + 1;
|
cp = strchr (cp, '\0') + 1;
|
||||||
}
|
}
|
||||||
while (*cp != '\0');
|
while (*cp != '\0');
|
||||||
assert (cnt == naudit_ifaces);
|
assert (cnt == naudit_ifaces);
|
||||||
|
@ -2690,8 +2690,7 @@ process_envvars (struct dl_main_state *state)
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
unsetenv (nextp);
|
unsetenv (nextp);
|
||||||
/* We could use rawmemchr but this need not be fast. */
|
nextp = strchr (nextp, '\0') + 1;
|
||||||
nextp = (char *) (strchr) (nextp, '\0') + 1;
|
|
||||||
}
|
}
|
||||||
while (*nextp != '\0');
|
while (*nextp != '\0');
|
||||||
|
|
||||||
|
|
|
@ -502,8 +502,8 @@ __gconv_read_conf (void)
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
const char *from = cp;
|
const char *from = cp;
|
||||||
const char *to = __rawmemchr (from, '\0') + 1;
|
const char *to = strchr (from, '\0') + 1;
|
||||||
cp = __rawmemchr (to, '\0') + 1;
|
cp = strchr (to, '\0') + 1;
|
||||||
|
|
||||||
add_alias2 (from, to, cp);
|
add_alias2 (from, to, cp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,7 +133,7 @@ gconv_init (struct __gconv_step *step)
|
||||||
|
|
||||||
enum variant var = 0;
|
enum variant var = 0;
|
||||||
for (const char *name = names; *name != '\0';
|
for (const char *name = names; *name != '\0';
|
||||||
name = __rawmemchr (name, '\0') + 1)
|
name = strchr (name, '\0') + 1)
|
||||||
{
|
{
|
||||||
if (__strcasecmp (step->__from_name, name) == 0)
|
if (__strcasecmp (step->__from_name, name) == 0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -189,7 +189,7 @@ gconv_init (struct __gconv_step *step)
|
||||||
|
|
||||||
enum variant var = 0;
|
enum variant var = 0;
|
||||||
for (const char *name = names; *name != '\0';
|
for (const char *name = names; *name != '\0';
|
||||||
name = __rawmemchr (name, '\0') + 1)
|
name = strchr (name, '\0') + 1)
|
||||||
{
|
{
|
||||||
if (__strcasecmp (step->__from_name, name) == 0)
|
if (__strcasecmp (step->__from_name, name) == 0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -217,11 +217,11 @@ nscd_getnetgrent (struct __netgrent *datap, char *buffer, size_t buflen,
|
||||||
|
|
||||||
datap->type = triple_val;
|
datap->type = triple_val;
|
||||||
datap->val.triple.host = get_nonempty_val (datap->cursor);
|
datap->val.triple.host = get_nonempty_val (datap->cursor);
|
||||||
datap->cursor = (char *) __rawmemchr (datap->cursor, '\0') + 1;
|
datap->cursor = strchr (datap->cursor, '\0') + 1;
|
||||||
datap->val.triple.user = get_nonempty_val (datap->cursor);
|
datap->val.triple.user = get_nonempty_val (datap->cursor);
|
||||||
datap->cursor = (char *) __rawmemchr (datap->cursor, '\0') + 1;
|
datap->cursor = strchr (datap->cursor, '\0') + 1;
|
||||||
datap->val.triple.domain = get_nonempty_val (datap->cursor);
|
datap->val.triple.domain = get_nonempty_val (datap->cursor);
|
||||||
datap->cursor = (char *) __rawmemchr (datap->cursor, '\0') + 1;
|
datap->cursor = strchr (datap->cursor, '\0') + 1;
|
||||||
|
|
||||||
return NSS_STATUS_SUCCESS;
|
return NSS_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1420,11 +1420,7 @@ plural_lookup (struct loaded_l10nfile *domain, unsigned long int n,
|
||||||
p = translation;
|
p = translation;
|
||||||
while (index-- > 0)
|
while (index-- > 0)
|
||||||
{
|
{
|
||||||
#ifdef _LIBC
|
|
||||||
p = __rawmemchr (p, '\0');
|
|
||||||
#else
|
|
||||||
p = strchr (p, '\0');
|
p = strchr (p, '\0');
|
||||||
#endif
|
|
||||||
/* And skip over the NUL byte. */
|
/* And skip over the NUL byte. */
|
||||||
p++;
|
p++;
|
||||||
|
|
||||||
|
|
2
io/ftw.c
2
io/ftw.c
|
@ -535,7 +535,7 @@ fail:
|
||||||
|
|
||||||
/* Next, update the `struct FTW' information. */
|
/* Next, update the `struct FTW' information. */
|
||||||
++data->ftw.level;
|
++data->ftw.level;
|
||||||
startp = __rawmemchr (data->dirbuf, '\0');
|
startp = strchr (data->dirbuf, '\0');
|
||||||
/* There always must be a directory name. */
|
/* There always must be a directory name. */
|
||||||
assert (startp != data->dirbuf);
|
assert (startp != data->dirbuf);
|
||||||
if (startp[-1] != '/')
|
if (startp[-1] != '/')
|
||||||
|
|
|
@ -38,7 +38,7 @@ _IO_str_init_static_internal (_IO_strfile *sf, char *ptr, size_t size,
|
||||||
char *end;
|
char *end;
|
||||||
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
end = __rawmemchr (ptr, '\0');
|
end = strchr (ptr, '\0');
|
||||||
else if ((size_t) ptr + size > (size_t) ptr)
|
else if ((size_t) ptr + size > (size_t) ptr)
|
||||||
end = ptr + size;
|
end = ptr + size;
|
||||||
else
|
else
|
||||||
|
|
|
@ -1726,15 +1726,7 @@ made an error in assuming that the byte @var{c} is present in the block.
|
||||||
In this case the result is unspecified. Otherwise the return value is a
|
In this case the result is unspecified. Otherwise the return value is a
|
||||||
pointer to the located byte.
|
pointer to the located byte.
|
||||||
|
|
||||||
This function is of special interest when looking for the end of a
|
When looking for the end of a string, use @code{strchr}.
|
||||||
string. Since all strings are terminated by a null byte a call like
|
|
||||||
|
|
||||||
@smallexample
|
|
||||||
rawmemchr (str, '\0')
|
|
||||||
@end smallexample
|
|
||||||
|
|
||||||
@noindent
|
|
||||||
will never go beyond the end of the string.
|
|
||||||
|
|
||||||
This function is a GNU extension.
|
This function is a GNU extension.
|
||||||
@end deftypefun
|
@end deftypefun
|
||||||
|
|
|
@ -32,7 +32,7 @@ nis_addmember (const_nis_name member, const_nis_name group)
|
||||||
nis_error status;
|
nis_error status;
|
||||||
char *cp, *cp2;
|
char *cp, *cp2;
|
||||||
|
|
||||||
cp = rawmemchr (nis_leaf_of_r (group, buf, sizeof (buf) - 1), '\0');
|
cp = strchr (nis_leaf_of_r (group, buf, sizeof (buf) - 1), '\0');
|
||||||
cp = stpcpy (cp, ".groups_dir");
|
cp = stpcpy (cp, ".groups_dir");
|
||||||
cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
|
cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
|
||||||
if (cp2 != NULL && cp2[0] != '\0')
|
if (cp2 != NULL && cp2[0] != '\0')
|
||||||
|
|
|
@ -483,7 +483,7 @@ rec_dirsearch (const_nis_name name, directory_obj *dir, nis_error *status)
|
||||||
}
|
}
|
||||||
while (nis_dir_cmp (domain, dir->do_name) != SAME_NAME);
|
while (nis_dir_cmp (domain, dir->do_name) != SAME_NAME);
|
||||||
|
|
||||||
cp = rawmemchr (leaf, '\0');
|
cp = strchr (leaf, '\0');
|
||||||
*cp++ = '.';
|
*cp++ = '.';
|
||||||
strcpy (cp, domain);
|
strcpy (cp, domain);
|
||||||
|
|
||||||
|
@ -614,7 +614,7 @@ nis_server_cache_search (const_nis_name name, int search_parent,
|
||||||
if (ret == NULL)
|
if (ret == NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
addr = rawmemchr (nis_server_cache[i]->name, '\0') + 8;
|
addr = strchr (nis_server_cache[i]->name, '\0') + 8;
|
||||||
addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7);
|
addr = (char *) ((uintptr_t) addr & ~(uintptr_t) 7);
|
||||||
xdrmem_create (&xdrs, addr, nis_server_cache[i]->size, XDR_DECODE);
|
xdrmem_create (&xdrs, addr, nis_server_cache[i]->size, XDR_DECODE);
|
||||||
if (!_xdr_directory_obj (&xdrs, ret))
|
if (!_xdr_directory_obj (&xdrs, ret))
|
||||||
|
|
|
@ -63,7 +63,7 @@ nis_local_directory (void)
|
||||||
__nisdomainname[0] = '\0';
|
__nisdomainname[0] = '\0';
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char *cp = rawmemchr (__nisdomainname, '\0');
|
char *cp = strchr (__nisdomainname, '\0');
|
||||||
|
|
||||||
/* Missing trailing dot? */
|
/* Missing trailing dot? */
|
||||||
if (cp[-1] != '.')
|
if (cp[-1] != '.')
|
||||||
|
@ -154,7 +154,7 @@ nis_local_host (void)
|
||||||
__nishostname[0] = '\0';
|
__nishostname[0] = '\0';
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char *cp = rawmemchr (__nishostname, '\0');
|
char *cp = strchr (__nishostname, '\0');
|
||||||
int len = cp - __nishostname;
|
int len = cp - __nishostname;
|
||||||
|
|
||||||
/* Hostname already fully qualified? */
|
/* Hostname already fully qualified? */
|
||||||
|
|
|
@ -32,7 +32,7 @@ nis_removemember (const_nis_name member, const_nis_name group)
|
||||||
nis_error status;
|
nis_error status;
|
||||||
char *cp, *cp2;
|
char *cp, *cp2;
|
||||||
|
|
||||||
cp = rawmemchr (nis_leaf_of_r (group, buf, sizeof (buf) - 1), '\0');
|
cp = strchr (nis_leaf_of_r (group, buf, sizeof (buf) - 1), '\0');
|
||||||
cp = stpcpy (cp, ".groups_dir");
|
cp = stpcpy (cp, ".groups_dir");
|
||||||
cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
|
cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
|
||||||
if (cp2 != NULL && cp2[0] != '\0')
|
if (cp2 != NULL && cp2[0] != '\0')
|
||||||
|
|
|
@ -1359,7 +1359,7 @@ cannot open /proc/self/cmdline: %m; disabling paranoia mode"));
|
||||||
for (char *cp = cmdline; cp < cmdline + readlen;)
|
for (char *cp = cmdline; cp < cmdline + readlen;)
|
||||||
{
|
{
|
||||||
argv[argc++] = cp;
|
argv[argc++] = cp;
|
||||||
cp = (char *) rawmemchr (cp, '\0') + 1;
|
cp = strchr (cp, '\0') + 1;
|
||||||
}
|
}
|
||||||
argv[argc] = NULL;
|
argv[argc] = NULL;
|
||||||
|
|
||||||
|
|
|
@ -259,7 +259,7 @@ cache_addgr (struct database_dyn *db, int fd, request_header *req,
|
||||||
/* Finally the stringified GID value. */
|
/* Finally the stringified GID value. */
|
||||||
memcpy (cp, buf, n);
|
memcpy (cp, buf, n);
|
||||||
char *key_copy = cp + key_offset;
|
char *key_copy = cp + key_offset;
|
||||||
assert (key_copy == (char *) rawmemchr (cp, '\0') + 1);
|
assert (key_copy == strchr (cp, '\0') + 1);
|
||||||
|
|
||||||
assert (cp == dataset->strdata + total - offsetof (struct dataset,
|
assert (cp == dataset->strdata + total - offsetof (struct dataset,
|
||||||
strdata));
|
strdata));
|
||||||
|
|
|
@ -453,14 +453,14 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
|
||||||
struct datahead *dh)
|
struct datahead *dh)
|
||||||
{
|
{
|
||||||
const char *group = key;
|
const char *group = key;
|
||||||
key = (char *) rawmemchr (key, '\0') + 1;
|
key = strchr (key, '\0') + 1;
|
||||||
size_t group_len = key - group;
|
size_t group_len = key - group;
|
||||||
const char *host = *key++ ? key : NULL;
|
const char *host = *key++ ? key : NULL;
|
||||||
if (host != NULL)
|
if (host != NULL)
|
||||||
key = (char *) rawmemchr (key, '\0') + 1;
|
key = strchr (key, '\0') + 1;
|
||||||
const char *user = *key++ ? key : NULL;
|
const char *user = *key++ ? key : NULL;
|
||||||
if (user != NULL)
|
if (user != NULL)
|
||||||
key = (char *) rawmemchr (key, '\0') + 1;
|
key = strchr (key, '\0') + 1;
|
||||||
const char *domain = *key++ ? key : NULL;
|
const char *domain = *key++ ? key : NULL;
|
||||||
|
|
||||||
if (__glibc_unlikely (debug_level > 0))
|
if (__glibc_unlikely (debug_level > 0))
|
||||||
|
@ -538,11 +538,11 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
|
||||||
match anything is stored in the netgroup cache. */
|
match anything is stored in the netgroup cache. */
|
||||||
if (host != NULL && *triplets != '\0')
|
if (host != NULL && *triplets != '\0')
|
||||||
success = strcmp (host, triplets) == 0;
|
success = strcmp (host, triplets) == 0;
|
||||||
triplets = (const char *) rawmemchr (triplets, '\0') + 1;
|
triplets = strchr (triplets, '\0') + 1;
|
||||||
|
|
||||||
if (success && user != NULL && *triplets != '\0')
|
if (success && user != NULL && *triplets != '\0')
|
||||||
success = strcmp (user, triplets) == 0;
|
success = strcmp (user, triplets) == 0;
|
||||||
triplets = (const char *) rawmemchr (triplets, '\0') + 1;
|
triplets = strchr (triplets, '\0') + 1;
|
||||||
|
|
||||||
if (success && (domain == NULL || *triplets == '\0'
|
if (success && (domain == NULL || *triplets == '\0'
|
||||||
|| strcmp (domain, triplets) == 0))
|
|| strcmp (domain, triplets) == 0))
|
||||||
|
@ -550,7 +550,7 @@ addinnetgrX (struct database_dyn *db, int fd, request_header *req,
|
||||||
dataset->resp.result = 1;
|
dataset->resp.result = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
triplets = (const char *) rawmemchr (triplets, '\0') + 1;
|
triplets = strchr (triplets, '\0') + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -243,7 +243,7 @@ cache_addpw (struct database_dyn *db, int fd, request_header *req,
|
||||||
/* Finally the stringified UID value. */
|
/* Finally the stringified UID value. */
|
||||||
memcpy (cp, buf, n);
|
memcpy (cp, buf, n);
|
||||||
char *key_copy = cp + key_offset;
|
char *key_copy = cp + key_offset;
|
||||||
assert (key_copy == (char *) rawmemchr (cp, '\0') + 1);
|
assert (key_copy == strchr (cp, '\0') + 1);
|
||||||
|
|
||||||
assert (cp == dataset->strdata + total - offsetof (struct dataset,
|
assert (cp == dataset->strdata + total - offsetof (struct dataset,
|
||||||
strdata));
|
strdata));
|
||||||
|
|
|
@ -269,7 +269,7 @@ CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
|
||||||
+ state.header->valstrlen);
|
+ state.header->valstrlen);
|
||||||
while (entidx < end)
|
while (entidx < end)
|
||||||
{
|
{
|
||||||
const char *next = rawmemchr (entidx, '\0') + 1;
|
const char *next = strchr (entidx, '\0') + 1;
|
||||||
size_t len = next - entidx;
|
size_t len = next - entidx;
|
||||||
|
|
||||||
if (len > buflen)
|
if (len > buflen)
|
||||||
|
|
|
@ -74,7 +74,6 @@
|
||||||
# endif
|
# endif
|
||||||
# define __mempcpy mempcpy
|
# define __mempcpy mempcpy
|
||||||
# define __pathconf pathconf
|
# define __pathconf pathconf
|
||||||
# define __rawmemchr rawmemchr
|
|
||||||
# define __readlink readlink
|
# define __readlink readlink
|
||||||
# define __stat stat
|
# define __stat stat
|
||||||
#endif
|
#endif
|
||||||
|
@ -232,7 +231,7 @@ realpath_stk (const char *name, char *resolved, struct realpath_bufs *bufs)
|
||||||
return NULL;
|
return NULL;
|
||||||
rname = bufs->rname.data;
|
rname = bufs->rname.data;
|
||||||
}
|
}
|
||||||
dest = __rawmemchr (rname, '\0');
|
dest = strchr (rname, '\0');
|
||||||
start = name;
|
start = name;
|
||||||
prefix_len = FILE_SYSTEM_PREFIX_LEN (rname);
|
prefix_len = FILE_SYSTEM_PREFIX_LEN (rname);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
|
||||||
void *
|
void *
|
||||||
RAWMEMCHR (const void *s, int c)
|
RAWMEMCHR (const void *s, int c)
|
||||||
{
|
{
|
||||||
if (c != '\0')
|
if ((unsigned char) c != '\0')
|
||||||
return memchr (s, c, (size_t)-1);
|
return memchr (s, c, (size_t)-1);
|
||||||
return (char *)s + strlen (s);
|
return (char *)s + strlen (s);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue