2016-08-26 20:28:46 +08:00
|
|
|
/* Alignment/padding coverage test for string comparison.
|
2023-01-07 05:08:04 +08:00
|
|
|
Copyright (C) 2016-2023 Free Software Foundation, Inc.
|
2016-08-26 20:28:46 +08:00
|
|
|
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 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2016-08-26 20:28:46 +08:00
|
|
|
|
|
|
|
/* 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>
|
2017-02-17 06:31:42 +08:00
|
|
|
#include <stdint.h>
|
2016-08-26 20:28:46 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-06-21 06:19:50 +08:00
|
|
|
#include <libc-diag.h>
|
2016-08-26 20:28:46 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-06-21 06:19:50 +08:00
|
|
|
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=");
|
2020-09-08 02:11:12 +08:00
|
|
|
#endif
|
|
|
|
#if __GNUC_PREREQ (11, 0)
|
|
|
|
/* Likewise GCC 11, with a different warning option. */
|
|
|
|
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
|
2018-06-21 06:19:50 +08:00
|
|
|
#endif
|
2016-08-26 20:28:46 +08:00
|
|
|
return strncmp (left, right, SIZE_MAX);
|
2018-06-21 06:19:50 +08:00
|
|
|
DIAG_POP_NEEDS_COMMENT;
|
2016-08-26 20:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
strncasecmp_max (const char *left, const char *right)
|
|
|
|
{
|
2018-06-21 06:19:50 +08:00
|
|
|
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=");
|
2020-09-08 02:11:12 +08:00
|
|
|
#endif
|
|
|
|
#if __GNUC_PREREQ (11, 0)
|
|
|
|
/* Likewise GCC 11, with a different warning option. */
|
|
|
|
DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overread");
|
2018-06-21 06:19:50 +08:00
|
|
|
#endif
|
2016-08-26 20:28:46 +08:00
|
|
|
return strncasecmp (left, right, SIZE_MAX);
|
2018-06-21 06:19:50 +08:00
|
|
|
DIAG_POP_NEEDS_COMMENT;
|
2016-08-26 20:28:46 +08:00
|
|
|
}
|
|
|
|
|
2017-03-23 22:32:17 +08:00
|
|
|
int
|
2016-08-26 20:28:46 +08:00
|
|
|
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. */
|
2017-07-07 01:01:03 +08:00
|
|
|
#define TIMEOUT 600
|
2016-08-26 20:28:46 +08:00
|
|
|
|
2017-03-23 22:32:17 +08:00
|
|
|
#include <support/test-driver.c>
|