(root)/
glibc-2.38/
string/
test-strerror-errno.c
       1  /* BZ #24024 strerror and errno test.
       2  
       3     Copyright (C) 2019-2023 Free Software Foundation, Inc.
       4     This file is part of the GNU C Library.
       5  
       6     The GNU C Library is free software; you can redistribute it and/or
       7     modify it under the terms of the GNU Lesser General Public
       8     License as published by the Free Software Foundation; either
       9     version 2.1 of the License, or (at your option) any later version.
      10  
      11     The GNU C Library is distributed in the hope that it will be useful,
      12     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14     Lesser General Public License for more details.
      15  
      16     You should have received a copy of the GNU Lesser General Public
      17     License along with the GNU C Library; if not, see
      18     <https://www.gnu.org/licenses/>.  */
      19  
      20  #include <dlfcn.h>
      21  #include <errno.h>
      22  #include <string.h>
      23  
      24  #include <support/check.h>
      25  #include <support/support.h>
      26  
      27  /* malloc is allowed to change errno to a value different than 0, even when
      28     there is no actual error.  This happens for example when the memory
      29     allocation through sbrk fails.  Simulate this by interposing our own
      30     malloc implementation which sets errno to ENOMEM and calls the original
      31     malloc.  */
      32  void
      33  *malloc (size_t size)
      34  {
      35    static void *(*real_malloc) (size_t size);
      36  
      37    if (!real_malloc)
      38      real_malloc = dlsym (RTLD_NEXT, "malloc");
      39  
      40    errno = ENOMEM;
      41  
      42    return (*real_malloc) (size);
      43  }
      44  
      45  /* strerror must not change the value of errno.  Unfortunately due to GCC bug
      46     #88576, this happens when -fmath-errno is used.  This simple test checks
      47     that it doesn't happen.  */
      48  static int
      49  do_test (void)
      50  {
      51    char *msg;
      52  
      53    errno = 0;
      54    msg = strerror (-3);
      55    (void) msg;
      56    TEST_COMPARE (errno, 0);
      57  
      58    locale_t l = xnewlocale (LC_ALL_MASK, "C", NULL);
      59    msg = strerror_l (-3, l);
      60    (void) msg;
      61    TEST_COMPARE (errno, 0);
      62  
      63    return 0;
      64  }
      65  
      66  #include <support/test-driver.c>