hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame string/test-strerror-errno.c

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