Blame malloc/tst-dynarray-at-fail.c

Packit 6c4009
/* Test reporting of out-of-bounds access for dynamic arrays.
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 "tst-dynarray-shared.h"
Packit 6c4009
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/capture_subprocess.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
Packit 6c4009
/* Run CALLBACK and check that the data on standard error equals
Packit 6c4009
   EXPECTED.  */
Packit 6c4009
static void
Packit 6c4009
check (const char *test, void (*callback) (void *), size_t index,
Packit 6c4009
       const char *expected)
Packit 6c4009
{
Packit 6c4009
  struct support_capture_subprocess result
Packit 6c4009
    = support_capture_subprocess (callback, &index);
Packit 6c4009
  if (strcmp (result.err.buffer, expected) != 0)
Packit 6c4009
    {
Packit 6c4009
      support_record_failure ();
Packit 6c4009
      printf ("error: test %s (%zu) unexpected standard error data\n"
Packit 6c4009
              "  expected: %s\n"
Packit 6c4009
              "  actual:   %s\n",
Packit 6c4009
              test, index, expected, result.err.buffer);
Packit 6c4009
    }
Packit 6c4009
  TEST_VERIFY (strlen (result.out.buffer) == 0);
Packit 6c4009
  TEST_VERIFY (WIFSIGNALED (result.status));
Packit 6c4009
  if (WIFSIGNALED (result.status))
Packit 6c4009
    TEST_VERIFY (WTERMSIG (result.status) == SIGABRT);
Packit 6c4009
  support_capture_subprocess_free (&result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Try indexing an empty array.  */
Packit 6c4009
static void
Packit 6c4009
test_empty (void *closure)
Packit 6c4009
{
Packit 6c4009
  size_t *pindex = closure;
Packit 6c4009
  struct dynarray_int dyn;
Packit 6c4009
  dynarray_int_init (&dyn);
Packit 6c4009
  dynarray_int_at (&dyn, *pindex);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Try indexing a one-element array.  */
Packit 6c4009
static void
Packit 6c4009
test_one (void *closure)
Packit 6c4009
{
Packit 6c4009
  size_t *pindex = closure;
Packit 6c4009
  struct dynarray_int dyn;
Packit 6c4009
  dynarray_int_init (&dyn);
Packit 6c4009
  TEST_VERIFY (dynarray_int_resize (&dyn, 1));
Packit 6c4009
  dynarray_int_at (&dyn, *pindex);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Try indexing a longer array.  */
Packit 6c4009
static void
Packit 6c4009
test_many (void *closure)
Packit 6c4009
{
Packit 6c4009
  size_t *pindex = closure;
Packit 6c4009
  struct dynarray_int dyn;
Packit 6c4009
  dynarray_int_init (&dyn);
Packit 6c4009
  TEST_VERIFY (dynarray_int_resize (&dyn, 5371));
Packit 6c4009
  dynarray_int_at (&dyn, *pindex);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* (size_t) -1 for use in string literals.  */
Packit 6c4009
#if SIZE_WIDTH == 32
Packit 6c4009
# define MINUS_1 "4294967295"
Packit 6c4009
#elif SIZE_WIDTH == 64
Packit 6c4009
# define MINUS_1 "18446744073709551615"
Packit 6c4009
#else
Packit 6c4009
# error "unknown value for SIZE_WIDTH"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  TEST_VERIFY (setenv ("LIBC_FATAL_STDERR_", "1", 1) == 0);
Packit 6c4009
Packit 6c4009
  check ("test_empty", test_empty, 0,
Packit 6c4009
         "Fatal glibc error: array index 0 not less than array length 0\n");
Packit 6c4009
  check ("test_empty", test_empty, 1,
Packit 6c4009
         "Fatal glibc error: array index 1 not less than array length 0\n");
Packit 6c4009
  check ("test_empty", test_empty, -1,
Packit 6c4009
         "Fatal glibc error: array index " MINUS_1
Packit 6c4009
         " not less than array length 0\n");
Packit 6c4009
Packit 6c4009
  check ("test_one", test_one, 1,
Packit 6c4009
         "Fatal glibc error: array index 1 not less than array length 1\n");
Packit 6c4009
  check ("test_one", test_one, 2,
Packit 6c4009
         "Fatal glibc error: array index 2 not less than array length 1\n");
Packit 6c4009
  check ("test_one", test_one, -1,
Packit 6c4009
         "Fatal glibc error: array index " MINUS_1
Packit 6c4009
         " not less than array length 1\n");
Packit 6c4009
Packit 6c4009
  check ("test_many", test_many, 5371,
Packit 6c4009
         "Fatal glibc error: array index 5371"
Packit 6c4009
         " not less than array length 5371\n");
Packit 6c4009
  check ("test_many", test_many, 5372,
Packit 6c4009
         "Fatal glibc error: array index 5372"
Packit 6c4009
         " not less than array length 5371\n");
Packit 6c4009
  check ("test_many", test_many, -1,
Packit 6c4009
         "Fatal glibc error: array index " MINUS_1
Packit 6c4009
         " not less than array length 5371\n");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>