Blame malloc/tst-mcheck.c

Packit Service 82fcde
/* Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <libc-diag.h>
Packit Service 82fcde
Packit Service 82fcde
static int errors = 0;
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
merror (const char *msg)
Packit Service 82fcde
{
Packit Service 82fcde
  ++errors;
Packit Service 82fcde
  printf ("Error: %s\n", msg);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  void *p, *q;
Packit Service 82fcde
Packit Service 82fcde
  errno = 0;
Packit Service 82fcde
Packit Service 82fcde
  DIAG_PUSH_NEEDS_COMMENT;
Packit Service 82fcde
#if __GNUC_PREREQ (7, 0)
Packit Service 82fcde
  /* GCC 7 warns about too-large allocations; here we want to test
Packit Service 82fcde
     that they fail.  */
Packit Service 82fcde
  DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
Packit Service 82fcde
#endif
Packit Service 82fcde
  p = malloc (-1);
Packit Service 82fcde
  DIAG_POP_NEEDS_COMMENT;
Packit Service 82fcde
Packit Service 82fcde
  if (p != NULL)
Packit Service 82fcde
    merror ("malloc (-1) succeeded.");
Packit Service 82fcde
  else if (errno != ENOMEM)
Packit Service 82fcde
    merror ("errno is not set correctly.");
Packit Service 82fcde
Packit Service 82fcde
  p = malloc (10);
Packit Service 82fcde
  if (p == NULL)
Packit Service 82fcde
    merror ("malloc (10) failed.");
Packit Service 82fcde
Packit Service 82fcde
  p = realloc (p, 0);
Packit Service 82fcde
  if (p != NULL)
Packit Service 82fcde
    merror ("realloc (p, 0) failed.");
Packit Service 82fcde
Packit Service 82fcde
  p = malloc (0);
Packit Service 82fcde
  if (p == NULL)
Packit Service 82fcde
    merror ("malloc (0) failed.");
Packit Service 82fcde
Packit Service 82fcde
  p = realloc (p, 0);
Packit Service 82fcde
  if (p != NULL)
Packit Service 82fcde
    merror ("realloc (p, 0) failed.");
Packit Service 82fcde
Packit Service 82fcde
  q = malloc (256);
Packit Service 82fcde
  if (q == NULL)
Packit Service 82fcde
    merror ("malloc (256) failed.");
Packit Service 82fcde
Packit Service 82fcde
  p = malloc (512);
Packit Service 82fcde
  if (p == NULL)
Packit Service 82fcde
    merror ("malloc (512) failed.");
Packit Service 82fcde
Packit Service 82fcde
  DIAG_PUSH_NEEDS_COMMENT;
Packit Service 82fcde
#if __GNUC_PREREQ (7, 0)
Packit Service 82fcde
  /* GCC 7 warns about too-large allocations; here we want to test
Packit Service 82fcde
     that they fail.  */
Packit Service 82fcde
  DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
Packit Service 82fcde
#endif
Packit Service 82fcde
  if (realloc (p, -256) != NULL)
Packit Service 82fcde
    merror ("realloc (p, -256) succeeded.");
Packit Service 82fcde
  else if (errno != ENOMEM)
Packit Service 82fcde
    merror ("errno is not set correctly.");
Packit Service 82fcde
  DIAG_POP_NEEDS_COMMENT;
Packit Service 82fcde
Packit Service 82fcde
  free (p);
Packit Service 82fcde
Packit Service 82fcde
  p = malloc (512);
Packit Service 82fcde
  if (p == NULL)
Packit Service 82fcde
    merror ("malloc (512) failed.");
Packit Service 82fcde
Packit Service 82fcde
  DIAG_PUSH_NEEDS_COMMENT;
Packit Service 82fcde
#if __GNUC_PREREQ (7, 0)
Packit Service 82fcde
  /* GCC 7 warns about too-large allocations; here we want to test
Packit Service 82fcde
     that they fail.  */
Packit Service 82fcde
  DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
Packit Service 82fcde
#endif
Packit Service 82fcde
  if (realloc (p, -1) != NULL)
Packit Service 82fcde
    merror ("realloc (p, -1) succeeded.");
Packit Service 82fcde
  else if (errno != ENOMEM)
Packit Service 82fcde
    merror ("errno is not set correctly.");
Packit Service 82fcde
  DIAG_POP_NEEDS_COMMENT;
Packit Service 82fcde
Packit Service 82fcde
  free (p);
Packit Service 82fcde
  free (q);
Packit Service 82fcde
Packit Service 82fcde
  return errors != 0;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#define TEST_FUNCTION do_test ()
Packit Service 82fcde
#include "../test-skeleton.c"