|
Packit Service |
a2ae7a |
/*
|
|
Packit Service |
a2ae7a |
* Copyright (C) 2008-2019 Free Software Foundation, Inc.
|
|
Packit Service |
a2ae7a |
* Written by Eric Blake and Bruno Haible
|
|
Packit Service |
a2ae7a |
*
|
|
Packit Service |
a2ae7a |
* This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
a2ae7a |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
a2ae7a |
* the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
a2ae7a |
* (at your option) any later version.
|
|
Packit Service |
a2ae7a |
*
|
|
Packit Service |
a2ae7a |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
a2ae7a |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
a2ae7a |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
a2ae7a |
* GNU General Public License for more details.
|
|
Packit Service |
a2ae7a |
*
|
|
Packit Service |
a2ae7a |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
a2ae7a |
* along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <config.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <string.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include "signature.h"
|
|
Packit Service |
a2ae7a |
SIGNATURE_CHECK (rawmemchr, void *, (void const *, int));
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <stdlib.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include "zerosize-ptr.h"
|
|
Packit Service |
a2ae7a |
#include "macros.h"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Calculating void * + int is not portable, so this wrapper converts
|
|
Packit Service |
a2ae7a |
to char * to make the tests easier to write. */
|
|
Packit Service |
a2ae7a |
#define RAWMEMCHR (char *) rawmemchr
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
int
|
|
Packit Service |
a2ae7a |
main (void)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
size_t n = 0x100000;
|
|
Packit Service |
a2ae7a |
char *input = malloc (n + 1);
|
|
Packit Service |
a2ae7a |
ASSERT (input);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
input[0] = 'a';
|
|
Packit Service |
a2ae7a |
input[1] = 'b';
|
|
Packit Service |
a2ae7a |
memset (input + 2, 'c', 1024);
|
|
Packit Service |
a2ae7a |
memset (input + 1026, 'd', n - 1028);
|
|
Packit Service |
a2ae7a |
input[n - 2] = 'e';
|
|
Packit Service |
a2ae7a |
input[n - 1] = 'a';
|
|
Packit Service |
a2ae7a |
input[n] = '\0';
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Basic behavior tests. */
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input, 'a') == input);
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input, 'b') == input + 1);
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input, 'c') == input + 2);
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input, 'd') == input + 1026);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input + 1, 'a') == input + n - 1);
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input + 1, 'e') == input + n - 2);
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input + 1, 0x789abc00 | 'e') == input + n - 2);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input, '\0') == input + n);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Alignment tests. */
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
int i, j;
|
|
Packit Service |
a2ae7a |
for (i = 0; i < 32; i++)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
for (j = 0; j < 256; j++)
|
|
Packit Service |
a2ae7a |
input[i + j] = j;
|
|
Packit Service |
a2ae7a |
for (j = 0; j < 256; j++)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (input + i, j) == input + i + j);
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Ensure that no unaligned oversized reads occur. */
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
char *page_boundary = (char *) zerosize_ptr ();
|
|
Packit Service |
a2ae7a |
size_t i;
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
if (!page_boundary)
|
|
Packit Service |
a2ae7a |
page_boundary = input + 4096;
|
|
Packit Service |
a2ae7a |
memset (page_boundary - 512, '1', 511);
|
|
Packit Service |
a2ae7a |
page_boundary[-1] = '2';
|
|
Packit Service |
a2ae7a |
for (i = 1; i <= 512; i++)
|
|
Packit Service |
a2ae7a |
ASSERT (RAWMEMCHR (page_boundary - i, (i * 0x01010100) | '2')
|
|
Packit Service |
a2ae7a |
== page_boundary - 1);
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
free (input);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
return 0;
|
|
Packit Service |
a2ae7a |
}
|