glibc/string/tst-cmp.c

236 lines
7.0 KiB
C
Raw Normal View History

/* Alignment/padding coverage test for string comparison.
Copyright (C) 2016-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
/* This performs test comparisons with various (mis)alignments and
characters in the padding. It is partly a regression test for bug
20327. */
#include <limits.h>
#include <malloc.h>
#include <stdbool.h>
Add missing header files throughout the testsuite. * crypt/md5.h: Test _LIBC with #if defined, not #if. * dirent/opendir-tst1.c: Include sys/stat.h. * dirent/tst-fdopendir.c: Include sys/stat.h. * dirent/tst-fdopendir2.c: Include stdlib.h. * dirent/tst-scandir.c: Include stdbool.h. * elf/tst-auditmod1.c: Include link.h and stddef.h. * elf/tst-tls15.c: Include stdlib.h. * elf/tst-tls16.c: Include stdlib.h. * elf/tst-tls17.c: Include stdlib.h. * elf/tst-tls18.c: Include stdlib.h. * iconv/tst-iconv6.c: Include endian.h. * iconvdata/bug-iconv11.c: Include limits.h. * io/test-utime.c: Include stdint.h. * io/tst-faccessat.c: Include sys/stat.h. * io/tst-fchmodat.c: Include sys/stat.h. * io/tst-fchownat.c: Include sys/stat.h. * io/tst-fstatat.c: Include sys/stat.h. * io/tst-futimesat.c: Include sys/stat.h. * io/tst-linkat.c: Include sys/stat.h. * io/tst-mkdirat.c: Include sys/stat.h and stdbool.h. * io/tst-mkfifoat.c: Include sys/stat.h and stdbool.h. * io/tst-mknodat.c: Include sys/stat.h and stdbool.h. * io/tst-openat.c: Include stdbool.h. * io/tst-readlinkat.c: Include sys/stat.h. * io/tst-renameat.c: Include sys/stat.h. * io/tst-symlinkat.c: Include sys/stat.h. * io/tst-unlinkat.c: Include stdbool.h. * libio/bug-memstream1.c: Include stdlib.h. * libio/bug-wmemstream1.c: Include stdlib.h. * libio/tst-fwrite-error.c: Include stdlib.h. * libio/tst-memstream1.c: Include stdlib.h. * libio/tst-memstream2.c: Include stdlib.h. * libio/tst-memstream3.c: Include stdlib.h. * malloc/tst-interpose-aux.c: Include stdint.h. * misc/tst-preadvwritev-common.c: Include sys/stat.h. * nptl/tst-basic7.c: Include limits.h. * nptl/tst-cancel25.c: Include pthread.h, not pthreadP.h. * nptl/tst-cancel4.c: Include stddef.h, limits.h, and sys/stat.h. * nptl/tst-cancel4_1.c: Include stddef.h. * nptl/tst-cancel4_2.c: Include stddef.h. * nptl/tst-cond16.c: Include limits.h. Use sysconf(_SC_PAGESIZE) instead of __getpagesize. * nptl/tst-cond18.c: Include limits.h. Use sysconf(_SC_PAGESIZE) instead of __getpagesize. * nptl/tst-cond4.c: Include stdint.h. * nptl/tst-cond6.c: Include stdint.h. * nptl/tst-stack2.c: Include limits.h. * nptl/tst-stackguard1.c: Include stddef.h. * nptl/tst-tls4.c: Include stdint.h. Don't include tls.h. * nptl/tst-tls4moda.c: Include stddef.h. Don't include stdio.h, unistd.h, or tls.h. * nptl/tst-tls4modb.c: Include stddef.h. Don't include stdio.h, unistd.h, or tls.h. * nptl/tst-tls5.h: Include stddef.h. Don't include stdlib.h or tls.h. * posix/tst-getaddrinfo2.c: Include stdio.h. * posix/tst-getaddrinfo5.c: Include stdio.h. * posix/tst-pathconf.c: Include sys/stat.h. * posix/tst-posix_fadvise-common.c: Include stdint.h. * posix/tst-preadwrite-common.c: Include sys/stat.h. * posix/tst-regex.c: Include stdint.h. Don't include spawn.h or spawn_int.h. * posix/tst-regexloc.c: Don't include spawn.h or spawn_int.h. * posix/tst-vfork3.c: Include sys/stat.h. * resolv/tst-bug18665-tcp.c: Include stdlib.h. * resolv/tst-res_hconf_reorder.c: Include stdlib.h. * resolv/tst-resolv-search.c: Include stdlib.h. * stdio-common/tst-fmemopen2.c: Include stdint.h. * stdio-common/tst-vfprintf-width-prec.c: Include stdlib.h. * stdlib/test-canon.c: Include sys/stat.h. * stdlib/tst-tls-atexit.c: Include stdbool.h. * string/test-memchr.c: Include stdint.h. * string/tst-cmp.c: Include stdint.h. * sysdeps/pthread/tst-timer.c: Include stdint.h. * sysdeps/unix/sysv/linux/tst-sync_file_range.c: Include stdint.h. * sysdeps/wordsize-64/tst-writev.c: Include limits.h and stdint.h. * sysdeps/x86_64/fpu/math-tests-arch.h: Include cpu-features.h. Don't include init-arch.h. * sysdeps/x86_64/multiarch/test-multiarch.h: Include cpu-features.h. Don't include init-arch.h. * sysdeps/x86_64/tst-auditmod10b.c: Include link.h and stddef.h. * sysdeps/x86_64/tst-auditmod3b.c: Include link.h and stddef.h. * sysdeps/x86_64/tst-auditmod4b.c: Include link.h and stddef.h. * sysdeps/x86_64/tst-auditmod5b.c: Include link.h and stddef.h. * sysdeps/x86_64/tst-auditmod6b.c: Include link.h and stddef.h. * sysdeps/x86_64/tst-auditmod6c.c: Include link.h and stddef.h. * sysdeps/x86_64/tst-auditmod7b.c: Include link.h and stddef.h. * time/clocktest.c: Include stdint.h. * time/tst-posixtz.c: Include stdint.h. * timezone/tst-timezone.c: Include stdint.h.
2017-02-16 22:31:42 +00:00
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <libc-diag.h>
static int
signum (int val)
{
if (val < 0)
return -1;
if (val > 0)
return 1;
else
return 0;
}
static size_t
max_size_t (size_t left, size_t right)
{
if (left > right)
return left;
else
return right;
}
/* Wrappers for strncmp and strncasecmp which determine the maximum
string length in some, either based on the input string length, or
using fixed constants. */
static int
strncmp_no_terminator (const char *left, const char *right)
{
size_t left_len = strlen (left);
size_t right_len = strlen (right);
return strncmp (left, right, max_size_t (left_len, right_len));
}
static int
strncasecmp_no_terminator (const char *left, const char *right)
{
size_t left_len = strlen (left);
size_t right_len = strlen (right);
return strncasecmp (left, right, max_size_t (left_len, right_len));
}
static int
strncmp_terminator (const char *left, const char *right)
{
size_t left_len = strlen (left);
size_t right_len = strlen (right);
return strncmp (left, right, max_size_t (left_len, right_len));
}
static int
strncasecmp_terminator (const char *left, const char *right)
{
size_t left_len = strlen (left);
size_t right_len = strlen (right);
return strncasecmp (left, right, max_size_t (left_len, right_len));
}
static int
strncmp_64 (const char *left, const char *right)
{
return strncmp (left, right, 64);
}
static int
strncasecmp_64 (const char *left, const char *right)
{
return strncasecmp (left, right, 64);
}
static int
strncmp_max (const char *left, const char *right)
{
DIAG_PUSH_NEEDS_COMMENT;
#if __GNUC_PREREQ (7, 0)
/* GCC 9 warns about the size passed to strncmp being larger than
PTRDIFF_MAX; the use of SIZE_MAX is deliberate here. */
DIAG_IGNORE_NEEDS_COMMENT (9, "-Wstringop-overflow=");
#endif
#if __GNUC_PREREQ (11, 0)
/* Likewise GCC 11, with a different warning option. */
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
#endif
return strncmp (left, right, SIZE_MAX);
DIAG_POP_NEEDS_COMMENT;
}
static int
strncasecmp_max (const char *left, const char *right)
{
DIAG_PUSH_NEEDS_COMMENT;
#if __GNUC_PREREQ (7, 0)
/* GCC 9 warns about the size passed to strncasecmp being larger
than PTRDIFF_MAX; the use of SIZE_MAX is deliberate here. */
DIAG_IGNORE_NEEDS_COMMENT (9, "-Wstringop-overflow=");
#endif
#if __GNUC_PREREQ (11, 0)
/* Likewise GCC 11, with a different warning option. */
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
#endif
return strncasecmp (left, right, SIZE_MAX);
DIAG_POP_NEEDS_COMMENT;
}
Update string tests to use the support test driver. Replaced all imports of test-skeleton.c with support/test-driver.c. In some cases it was needed to adjust do_test to return int instead of static int since that is the method's signature expected by test-driver.c. Checked on x86_64. * string/test-string.h (TEST_FUNCTION): Use test_main instead of test_main (). (CMDLINE_PROCESS): Use function instead of defined macro. * debug/test-strcpy_chk.c: Import support/test-driver.c and also <suppport/support.h> to use set_fortify_handler(). * string/bug-envz1.c: Import support/test-driver.c instead of test-skeleton.c. * string/bug-strcoll2.c: Likewise. * string/bug-strtok1.c: Likewise. * string/stratcliff.c: Likewise. * string/test-ffs.c: Likewise. * string/test-memccpy.c: Likewise. * string/test-memchr.c: Likewise. * string/test-memcmp.c: Likewise. * string/test-memcpy.c: Likewise. * string/test-memmem.c: Likewise. * string/test-memmove.c: Likewise. * string/test-memrchr.c: Likewise. * string/test-memset.c: Likewise. * string/test-rawmemchr.c: Likewise. * string/test-strcasecmp.c: Likewise. * string/test-strcasestr.c: Likewise. * string/test-strcat.c: Likewise. * string/test-strchr.c: Likewise. * string/test-strcmp.c: Likewise. * string/test-strcpy.c: Likewise. * string/test-string.h: Likewise. * string/test-strlen.c: Likewise. * string/test-strncasecmp.c: Likewise. * string/test-strncat.c: Likewise. * string/test-strncmp.c: Likewise. * string/test-strncpy.c: Likewise. * string/test-strnlen.c: Likewise. * string/test-strpbrk.c: Likewise. * string/test-strrchr.c: Likewise. * string/test-strspn.c: Likewise. * string/test-strstr.c: Likewise. * string/tst-bswap.c: Likewise. * string/tst-cmp.c: Likewise. * string/tst-endian.c: Likewise. * string/tst-inlcall.c: Likewise. * string/tst-strcoll-overflow.c: Likewise. * string/tst-strfry.c: Likewise. * string/tst-strlen.c: Likewise. * string/tst-strtok.c: Likewise. * string/tst-strtok_r.c: Likewise. * string/tst-strxfrm.c: Likewise. * string/tst-strxfrm2.c: Likewise. * string/tst-svc.c: Likewise. * string/tst-svc2.c: Likewise.
2017-03-23 14:32:17 +00:00
int
do_test (void)
{
enum {
max_align = 64,
max_string_length = 33
};
size_t blob_size = max_align + max_string_length + 1;
char *left = memalign (max_align, blob_size);
char *right = memalign (max_align, blob_size);
if (left == NULL || right == NULL)
{
printf ("error: out of memory\n");
return 1;
}
const struct
{
const char *name;
int (*implementation) (const char *, const char *);
} functions[] =
{
{ "strcmp", strcmp },
{ "strcasecmp", strcasecmp },
{ "strncmp (without NUL)", strncmp_no_terminator},
{ "strncasecmp (without NUL)", strncasecmp_no_terminator},
{ "strncmp (with NUL)", strncmp_terminator},
{ "strncasecmp (with NUL)", strncasecmp_terminator},
{ "strncmp (length 64)", strncmp_64},
{ "strncasecmp (length 64)", strncasecmp_64},
{ "strncmp (length SIZE_MAX)", strncmp_max},
{ "strncasecmp (length SIZE_MAX)", strncasecmp_max},
{ NULL, NULL }
};
const char *const strings[] =
{
"",
"0",
"01",
"01234567",
"0123456789abcde",
"0123456789abcdef",
"0123456789abcdefg",
"1",
"10",
"123456789abcdef",
"123456789abcdefg",
"23456789abcdef",
"23456789abcdefg",
"abcdefghijklmnopqrstuvwxyzABCDEF",
NULL
};
const unsigned char pads[] =
{ 0, 1, 32, 64, 128, '0', '1', 'e', 'f', 'g', 127, 192, 255 };
bool errors = false;
for (int left_idx = 0; strings[left_idx] != NULL; ++left_idx)
for (int left_align = 0; left_align < max_align; ++left_align)
for (unsigned pad_left = 0; pad_left < sizeof (pads); ++pad_left)
{
memset (left, pads[pad_left], blob_size);
strcpy (left + left_align, strings[left_idx]);
for (int right_idx = 0; strings[right_idx] != NULL; ++right_idx)
for (unsigned pad_right = 0; pad_right < sizeof (pads);
++pad_right)
for (int right_align = 0; right_align < max_align;
++right_align)
{
memset (right, pads[pad_right], blob_size);
strcpy (right + right_align, strings[right_idx]);
for (int func = 0; functions[func].name != NULL; ++func)
{
int expected = left_idx - right_idx;
int actual = functions[func].implementation
(left + left_align, right + right_align);
if (signum (actual) != signum (expected))
{
printf ("error: mismatch for %s: %d\n"
" left: \"%s\"\n"
" right: \"%s\"\n"
" pad_left = %u, pad_right = %u,\n"
" left_align = %d, right_align = %d\n",
functions[func].name, actual,
strings[left_idx], strings[right_idx],
pad_left, pad_right,
left_align, right_align);
errors = true;
}
}
}
}
free (right);
free (left);
return errors;
}
/* The nested loops need a long time to complete on slower
machines. */
#define TIMEOUT 600
Update string tests to use the support test driver. Replaced all imports of test-skeleton.c with support/test-driver.c. In some cases it was needed to adjust do_test to return int instead of static int since that is the method's signature expected by test-driver.c. Checked on x86_64. * string/test-string.h (TEST_FUNCTION): Use test_main instead of test_main (). (CMDLINE_PROCESS): Use function instead of defined macro. * debug/test-strcpy_chk.c: Import support/test-driver.c and also <suppport/support.h> to use set_fortify_handler(). * string/bug-envz1.c: Import support/test-driver.c instead of test-skeleton.c. * string/bug-strcoll2.c: Likewise. * string/bug-strtok1.c: Likewise. * string/stratcliff.c: Likewise. * string/test-ffs.c: Likewise. * string/test-memccpy.c: Likewise. * string/test-memchr.c: Likewise. * string/test-memcmp.c: Likewise. * string/test-memcpy.c: Likewise. * string/test-memmem.c: Likewise. * string/test-memmove.c: Likewise. * string/test-memrchr.c: Likewise. * string/test-memset.c: Likewise. * string/test-rawmemchr.c: Likewise. * string/test-strcasecmp.c: Likewise. * string/test-strcasestr.c: Likewise. * string/test-strcat.c: Likewise. * string/test-strchr.c: Likewise. * string/test-strcmp.c: Likewise. * string/test-strcpy.c: Likewise. * string/test-string.h: Likewise. * string/test-strlen.c: Likewise. * string/test-strncasecmp.c: Likewise. * string/test-strncat.c: Likewise. * string/test-strncmp.c: Likewise. * string/test-strncpy.c: Likewise. * string/test-strnlen.c: Likewise. * string/test-strpbrk.c: Likewise. * string/test-strrchr.c: Likewise. * string/test-strspn.c: Likewise. * string/test-strstr.c: Likewise. * string/tst-bswap.c: Likewise. * string/tst-cmp.c: Likewise. * string/tst-endian.c: Likewise. * string/tst-inlcall.c: Likewise. * string/tst-strcoll-overflow.c: Likewise. * string/tst-strfry.c: Likewise. * string/tst-strlen.c: Likewise. * string/tst-strtok.c: Likewise. * string/tst-strtok_r.c: Likewise. * string/tst-strxfrm.c: Likewise. * string/tst-strxfrm2.c: Likewise. * string/tst-svc.c: Likewise. * string/tst-svc2.c: Likewise.
2017-03-23 14:32:17 +00:00
#include <support/test-driver.c>