Blame string/test-strerror-errno.c

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