Blame string/test-strerror-errno.c

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