| 1 |
/* |
| 2 |
* Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 3 |
* Written by Eric Blake and Bruno Haible |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
#include <config.h> |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "signature.h" |
| 23 |
SIGNATURE_CHECK (memchr, void *, (void const *, int, size_t)); |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
#include "zerosize-ptr.h" |
| 28 |
#include "macros.h" |
| 29 |
|
| 30 |
/* Test the library, not the compiler+library. */ |
| 31 |
static void * |
| 32 |
lib_memchr (void const *s, int c, size_t n) |
| 33 |
{ |
| 34 |
return memchr (s, c, n); |
| 35 |
} |
| 36 |
static void *(*volatile volatile_memchr) (void const *, int, size_t) |
| 37 |
= lib_memchr; |
| 38 |
#undef memchr |
| 39 |
#define memchr volatile_memchr |
| 40 |
|
| 41 |
int |
| 42 |
main (void) |
| 43 |
{ |
| 44 |
size_t n = 0x100000; |
| 45 |
char *input = malloc (n); |
| 46 |
ASSERT (input); |
| 47 |
|
| 48 |
input[0] = 'a'; |
| 49 |
input[1] = 'b'; |
| 50 |
memset (input + 2, 'c', 1024); |
| 51 |
memset (input + 1026, 'd', n - 1028); |
| 52 |
input[n - 2] = 'e'; |
| 53 |
input[n - 1] = 'a'; |
| 54 |
|
| 55 |
/* Basic behavior tests. */ |
| 56 |
ASSERT (memchr (input, 'a', n) == input); |
| 57 |
|
| 58 |
ASSERT (memchr (input, 'a', 0) == NULL); |
| 59 |
|
| 60 |
{ |
| 61 |
void *page_boundary = zerosize_ptr (); |
| 62 |
if (page_boundary) |
| 63 |
ASSERT (memchr (page_boundary, 'a', 0) == NULL); |
| 64 |
} |
| 65 |
|
| 66 |
ASSERT (memchr (input, 'b', n) == input + 1); |
| 67 |
ASSERT (memchr (input, 'c', n) == input + 2); |
| 68 |
ASSERT (memchr (input, 'd', n) == input + 1026); |
| 69 |
|
| 70 |
ASSERT (memchr (input + 1, 'a', n - 1) == input + n - 1); |
| 71 |
ASSERT (memchr (input + 1, 'e', n - 1) == input + n - 2); |
| 72 |
ASSERT (memchr (input + 1, 0x789abc00 | 'e', n - 1) == input + n - 2); |
| 73 |
|
| 74 |
ASSERT (memchr (input, 'f', n) == NULL); |
| 75 |
ASSERT (memchr (input, '\0', n) == NULL); |
| 76 |
|
| 77 |
/* Check that a very long haystack is handled quickly if the byte is |
| 78 |
found near the beginning. */ |
| 79 |
{ |
| 80 |
size_t repeat = 10000; |
| 81 |
for (; repeat > 0; repeat--) |
| 82 |
{ |
| 83 |
ASSERT (memchr (input, 'c', n) == input + 2); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/* Alignment tests. */ |
| 88 |
{ |
| 89 |
int i, j; |
| 90 |
for (i = 0; i < 32; i++) |
| 91 |
{ |
| 92 |
for (j = 0; j < 256; j++) |
| 93 |
input[i + j] = j; |
| 94 |
for (j = 0; j < 256; j++) |
| 95 |
{ |
| 96 |
ASSERT (memchr (input + i, j, 256) == input + i + j); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/* Check that memchr() does not read past the first occurrence of the |
| 102 |
byte being searched. See the Austin Group's clarification |
| 103 |
<https://www.opengroup.org/austin/docs/austin_454.txt>. |
| 104 |
Test both '\0' and something else, since some implementations |
| 105 |
special-case searching for NUL. |
| 106 |
*/ |
| 107 |
{ |
| 108 |
char *page_boundary = (char *) zerosize_ptr (); |
| 109 |
/* Too small, and we miss cache line boundary tests; too large, |
| 110 |
and the test takes cubically longer to complete. */ |
| 111 |
int limit = 257; |
| 112 |
|
| 113 |
if (page_boundary != NULL) |
| 114 |
{ |
| 115 |
for (n = 1; n <= limit; n++) |
| 116 |
{ |
| 117 |
char *mem = page_boundary - n; |
| 118 |
memset (mem, 'X', n); |
| 119 |
ASSERT (memchr (mem, 'U', n) == NULL); |
| 120 |
ASSERT (memchr (mem, 0, n) == NULL); |
| 121 |
|
| 122 |
{ |
| 123 |
size_t i; |
| 124 |
size_t k; |
| 125 |
|
| 126 |
for (i = 0; i < n; i++) |
| 127 |
{ |
| 128 |
mem[i] = 'U'; |
| 129 |
for (k = i + 1; k < n + limit; k++) |
| 130 |
ASSERT (memchr (mem, 'U', k) == mem + i); |
| 131 |
mem[i] = 0; |
| 132 |
for (k = i + 1; k < n + limit; k++) |
| 133 |
ASSERT (memchr (mem, 0, k) == mem + i); |
| 134 |
mem[i] = 'X'; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
free (input); |
| 142 |
|
| 143 |
/* Test zero-length operations on NULL pointers, allowed by |
| 144 |
<https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf>. */ |
| 145 |
ASSERT (memchr (NULL, '?', 0) == NULL); |
| 146 |
|
| 147 |
return test_exit_status; |
| 148 |
} |