mirror of
https://git.savannah.gnu.org/git/gnulib.git
synced 2025-08-10 04:43:00 +03:00
* doc/glibc-functions/reallocarray.texi (reallocarray): Mention ptrdiff_t overflow. * lib/reallocarray.c (reallocarray): Reindent as per usual GNU. * lib/stdlib.in.h (reallocarray): Allow reallocarray to be replaced. * m4/reallocarray.m4 (gl_FUNC_REALLOCARRAY): Check for ptrdiff_t overflow. * m4/stdlib_h.m4 (gl_STDLIB_H_DEFAULTS): Set up REPLACE_REALLOCARRAY. * modules/reallocarray (Files): Add malloc.m4. (configure.ac): Also test REPLACE_REALLOCARRAY. * modules/reallocarray-tests (Depends-on): Add stdint. * modules/stdlib (stdlib.h): Substitute REPLACE_REALLOCARRAY. * tests/test-reallocarray.c: Include stdint.h. (main): Check for ptrdiff_t overflow.
50 lines
1.4 KiB
C
50 lines
1.4 KiB
C
/* Test of reallocarray function.
|
|
Copyright (C) 2010-2021 Free Software Foundation, Inc.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
|
|
#include "signature.h"
|
|
SIGNATURE_CHECK (reallocarray, void *, (void *, size_t, size_t));
|
|
|
|
int
|
|
main ()
|
|
{
|
|
void *volatile p = NULL;
|
|
|
|
/* Check that reallocarray fails when requested to allocate a block
|
|
of memory larger than PTRDIFF_MAX or SIZE_MAX bytes. */
|
|
for (size_t n = 2; n != 0; n <<= 1)
|
|
{
|
|
p = reallocarray (p, PTRDIFF_MAX / n + 1, n);
|
|
if (p)
|
|
return 1;
|
|
if (errno != ENOMEM)
|
|
return 2;
|
|
|
|
p = reallocarray (p, SIZE_MAX / n + 1, n);
|
|
if (p)
|
|
return 3;
|
|
if (errno != ENOMEM)
|
|
return 4;
|
|
}
|
|
|
|
return 0;
|
|
}
|