Blame malloc/tst-reallocarray.c

Packit 6c4009
/* Test for reallocarray.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <malloc.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  void *ptr = NULL;
Packit 6c4009
  void *ptr2 = NULL;
Packit 6c4009
  unsigned char *c;
Packit 6c4009
  size_t i;
Packit 6c4009
  int ok;
Packit 6c4009
  const size_t max = ~(size_t)0;
Packit 6c4009
  size_t a, b;
Packit 6c4009
Packit 6c4009
  /* Test overflow detection.  */
Packit 6c4009
  errno = 0;
Packit 6c4009
  ptr = reallocarray (NULL, max, 2);
Packit 6c4009
  TEST_VERIFY (!ptr);
Packit 6c4009
  TEST_VERIFY (errno == ENOMEM);
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  ptr = reallocarray (NULL, 2, max);
Packit 6c4009
  TEST_VERIFY (!ptr);
Packit 6c4009
  TEST_VERIFY (errno == ENOMEM);
Packit 6c4009
Packit 6c4009
  a = 65537;
Packit 6c4009
  b = max/65537 + 1;
Packit 6c4009
  errno = 0;
Packit 6c4009
  ptr = reallocarray (NULL, a, b);
Packit 6c4009
  TEST_VERIFY (!ptr);
Packit 6c4009
  TEST_VERIFY (errno == ENOMEM);
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  ptr = reallocarray (NULL, b, a);
Packit 6c4009
  TEST_VERIFY (!ptr);
Packit 6c4009
  TEST_VERIFY (errno == ENOMEM);
Packit 6c4009
Packit 6c4009
  /* Test realloc-like behavior.  */
Packit 6c4009
  /* Allocate memory like malloc.  */
Packit 6c4009
  ptr = reallocarray (NULL, 10, 2);
Packit 6c4009
  TEST_VERIFY_EXIT (ptr);
Packit 6c4009
  TEST_VERIFY_EXIT (malloc_usable_size (ptr) >= 10*2);
Packit 6c4009
Packit 6c4009
  memset (ptr, 0xAF, 10*2);
Packit 6c4009
Packit 6c4009
  /* Enlarge buffer.   */
Packit 6c4009
  ptr2 = reallocarray (ptr, 20, 2);
Packit 6c4009
  TEST_VERIFY (ptr2);
Packit 6c4009
  if (ptr2)
Packit 6c4009
    ptr = ptr2;
Packit 6c4009
  TEST_VERIFY (malloc_usable_size (ptr) >= 20*2);
Packit 6c4009
Packit 6c4009
  c = ptr;
Packit 6c4009
  ok = 1;
Packit 6c4009
  for (i = 0; i < 10*2; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (c[i] != 0xAF)
Packit 6c4009
        ok = 0;
Packit 6c4009
    }
Packit 6c4009
  TEST_VERIFY (ok);
Packit 6c4009
Packit 6c4009
  /* Decrease buffer size.  */
Packit 6c4009
  ptr2 = reallocarray (ptr, 5, 3);
Packit 6c4009
  TEST_VERIFY (ptr2);
Packit 6c4009
  if (ptr2)
Packit 6c4009
    ptr = ptr2;
Packit 6c4009
  TEST_VERIFY_EXIT (malloc_usable_size (ptr) >= 5*3);
Packit 6c4009
Packit 6c4009
  c = ptr;
Packit 6c4009
  ok = 1;
Packit 6c4009
  for (i = 0; i < 5*3; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (c[i] != 0xAF)
Packit 6c4009
        ok = 0;
Packit 6c4009
    }
Packit 6c4009
  TEST_VERIFY (ok);
Packit 6c4009
Packit 6c4009
  /* Overflow should leave buffer untouched.  */
Packit 6c4009
  errno = 0;
Packit 6c4009
  ptr2 = reallocarray (ptr, 2, ~(size_t)0);
Packit 6c4009
  TEST_VERIFY (!ptr2);
Packit 6c4009
  TEST_VERIFY (errno == ENOMEM);
Packit 6c4009
Packit 6c4009
  c = ptr;
Packit 6c4009
  ok = 1;
Packit 6c4009
  for (i = 0; i < 5*3; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (c[i] != 0xAF)
Packit 6c4009
        ok = 0;
Packit 6c4009
    }
Packit 6c4009
  TEST_VERIFY (ok);
Packit 6c4009
Packit 6c4009
  free (ptr);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>