Blame string/test-strerror-errno.c

Packit Bot ffd7f5
/* BZ #24024 strerror and errno test.
Packit Bot ffd7f5
Packit Bot ffd7f5
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Bot ffd7f5
   This file is part of the GNU C Library.
Packit Bot ffd7f5
Packit Bot ffd7f5
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot ffd7f5
   modify it under the terms of the GNU Lesser General Public
Packit Bot ffd7f5
   License as published by the Free Software Foundation; either
Packit Bot ffd7f5
   version 2.1 of the License, or (at your option) any later version.
Packit Bot ffd7f5
Packit Bot ffd7f5
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot ffd7f5
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot ffd7f5
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot ffd7f5
   Lesser General Public License for more details.
Packit Bot ffd7f5
Packit Bot ffd7f5
   You should have received a copy of the GNU Lesser General Public
Packit Bot ffd7f5
   License along with the GNU C Library; if not, see
Packit Bot ffd7f5
   <http://www.gnu.org/licenses/>.  */
Packit Bot ffd7f5
Packit Bot ffd7f5
#include <dlfcn.h>
Packit Bot ffd7f5
#include <errno.h>
Packit Bot ffd7f5
#include <string.h>
Packit Bot ffd7f5
Packit Bot ffd7f5
#include <support/check.h>
Packit Bot ffd7f5
#include <support/support.h>
Packit Bot ffd7f5
Packit Bot ffd7f5
/* malloc is allowed to change errno to a value different than 0, even when
Packit Bot ffd7f5
   there is no actual error.  This happens for example when the memory
Packit Bot ffd7f5
   allocation through sbrk fails.  Simulate this by interposing our own
Packit Bot ffd7f5
   malloc implementation which sets errno to ENOMEM and calls the original
Packit Bot ffd7f5
   malloc.  */
Packit Bot ffd7f5
void
Packit Bot ffd7f5
*malloc (size_t size)
Packit Bot ffd7f5
{
Packit Bot ffd7f5
  static void *(*real_malloc) (size_t size);
Packit Bot ffd7f5
Packit Bot ffd7f5
  if (!real_malloc)
Packit Bot ffd7f5
    real_malloc = dlsym (RTLD_NEXT, "malloc");
Packit Bot ffd7f5
Packit Bot ffd7f5
  errno = ENOMEM;
Packit Bot ffd7f5
Packit Bot ffd7f5
  return (*real_malloc) (size);
Packit Bot ffd7f5
}
Packit Bot ffd7f5
Packit Bot ffd7f5
/* strerror must not change the value of errno.  Unfortunately due to GCC bug
Packit Bot ffd7f5
   #88576, this happens when -fmath-errno is used.  This simple test checks
Packit Bot ffd7f5
   that it doesn't happen.  */
Packit Bot ffd7f5
static int
Packit Bot ffd7f5
do_test (void)
Packit Bot ffd7f5
{
Packit Bot ffd7f5
  char *msg;
Packit Bot ffd7f5
Packit Bot ffd7f5
  errno = 0;
Packit Bot ffd7f5
  msg = strerror (-3);
Packit Bot ffd7f5
  (void) msg;
Packit Bot ffd7f5
  TEST_COMPARE (errno, 0);
Packit Bot ffd7f5
Packit Bot ffd7f5
  return 0;
Packit Bot ffd7f5
}
Packit Bot ffd7f5
Packit Bot ffd7f5
#include <support/test-driver.c>