(root)/
glibc-2.38/
malloc/
tst-dynarray-at-fail.c
       1  /* Test reporting of out-of-bounds access for dynamic arrays.
       2     Copyright (C) 2017-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include "tst-dynarray-shared.h"
      20  
      21  #include <signal.h>
      22  #include <stdint.h>
      23  #include <string.h>
      24  #include <support/capture_subprocess.h>
      25  #include <support/check.h>
      26  
      27  /* Run CALLBACK and check that the data on standard error equals
      28     EXPECTED.  */
      29  static void
      30  check (const char *test, void (*callback) (void *), size_t index,
      31         const char *expected)
      32  {
      33    struct support_capture_subprocess result
      34      = support_capture_subprocess (callback, &index);
      35    if (strcmp (result.err.buffer, expected) != 0)
      36      {
      37        support_record_failure ();
      38        printf ("error: test %s (%zu) unexpected standard error data\n"
      39                "  expected: %s\n"
      40                "  actual:   %s\n",
      41                test, index, expected, result.err.buffer);
      42      }
      43    TEST_VERIFY (strlen (result.out.buffer) == 0);
      44    TEST_VERIFY (WIFSIGNALED (result.status));
      45    if (WIFSIGNALED (result.status))
      46      TEST_VERIFY (WTERMSIG (result.status) == SIGABRT);
      47    support_capture_subprocess_free (&result);
      48  }
      49  
      50  /* Try indexing an empty array.  */
      51  static void
      52  test_empty (void *closure)
      53  {
      54    size_t *pindex = closure;
      55    struct dynarray_int dyn;
      56    dynarray_int_init (&dyn);
      57    dynarray_int_at (&dyn, *pindex);
      58  }
      59  
      60  /* Try indexing a one-element array.  */
      61  static void
      62  test_one (void *closure)
      63  {
      64    size_t *pindex = closure;
      65    struct dynarray_int dyn;
      66    dynarray_int_init (&dyn);
      67    TEST_VERIFY (dynarray_int_resize (&dyn, 1));
      68    dynarray_int_at (&dyn, *pindex);
      69  }
      70  
      71  /* Try indexing a longer array.  */
      72  static void
      73  test_many (void *closure)
      74  {
      75    size_t *pindex = closure;
      76    struct dynarray_int dyn;
      77    dynarray_int_init (&dyn);
      78    TEST_VERIFY (dynarray_int_resize (&dyn, 5371));
      79    dynarray_int_at (&dyn, *pindex);
      80  }
      81  
      82  /* (size_t) -1 for use in string literals.  */
      83  #if SIZE_WIDTH == 32
      84  # define MINUS_1 "4294967295"
      85  #elif SIZE_WIDTH == 64
      86  # define MINUS_1 "18446744073709551615"
      87  #else
      88  # error "unknown value for SIZE_WIDTH"
      89  #endif
      90  
      91  static int
      92  do_test (void)
      93  {
      94    TEST_VERIFY (setenv ("LIBC_FATAL_STDERR_", "1", 1) == 0);
      95  
      96    check ("test_empty", test_empty, 0,
      97           "Fatal glibc error: array index 0 not less than array length 0\n");
      98    check ("test_empty", test_empty, 1,
      99           "Fatal glibc error: array index 1 not less than array length 0\n");
     100    check ("test_empty", test_empty, -1,
     101           "Fatal glibc error: array index " MINUS_1
     102           " not less than array length 0\n");
     103  
     104    check ("test_one", test_one, 1,
     105           "Fatal glibc error: array index 1 not less than array length 1\n");
     106    check ("test_one", test_one, 2,
     107           "Fatal glibc error: array index 2 not less than array length 1\n");
     108    check ("test_one", test_one, -1,
     109           "Fatal glibc error: array index " MINUS_1
     110           " not less than array length 1\n");
     111  
     112    check ("test_many", test_many, 5371,
     113           "Fatal glibc error: array index 5371"
     114           " not less than array length 5371\n");
     115    check ("test_many", test_many, 5372,
     116           "Fatal glibc error: array index 5372"
     117           " not less than array length 5371\n");
     118    check ("test_many", test_many, -1,
     119           "Fatal glibc error: array index " MINUS_1
     120           " not less than array length 5371\n");
     121  
     122    return 0;
     123  }
     124  
     125  #include <support/test-driver.c>