Blame string/test-strerror-errno.c

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