Blame assert/assert.c

Packit Service 82fcde
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <assert.h>
Packit Service 82fcde
#include <atomic.h>
Packit Service 82fcde
#include <ldsodefs.h>
Packit Service 82fcde
#include <libintl.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <sysdep.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <sys/mman.h>
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
extern const char *__progname;
Packit Service 82fcde
Packit Service 82fcde
#include <wchar.h>
Packit Service 82fcde
#include <libio/iolibio.h>
Packit Service 82fcde
#define fflush(s) _IO_fflush (s)
Packit Service 82fcde
Packit Service 82fcde
/* This function, when passed a string containing an asserted
Packit Service 82fcde
   expression, a filename, and a line number, prints a message
Packit Service 82fcde
   on the standard error stream of the form:
Packit Service 82fcde
	a.c:10: foobar: Assertion `a == b' failed.
Packit Service 82fcde
   It then aborts program execution via a call to `abort'.  */
Packit Service 82fcde
Packit Service 82fcde
#ifdef FATAL_PREPARE_INCLUDE
Packit Service 82fcde
# include FATAL_PREPARE_INCLUDE
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
__assert_fail_base (const char *fmt, const char *assertion, const char *file,
Packit Service 82fcde
		    unsigned int line, const char *function)
Packit Service 82fcde
{
Packit Service 82fcde
  char *str;
Packit Service 82fcde
Packit Service 82fcde
#ifdef FATAL_PREPARE
Packit Service 82fcde
  FATAL_PREPARE;
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
  int total;
Packit Service 82fcde
  if (__asprintf (&str, fmt,
Packit Service 82fcde
		  __progname, __progname[0] ? ": " : "",
Packit Service 82fcde
		  file, line,
Packit Service 82fcde
		  function ? function : "", function ? ": " : "",
Packit Service 82fcde
		  assertion, &total) >= 0)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Print the message.  */
Packit Service 82fcde
      (void) __fxprintf (NULL, "%s", str);
Packit Service 82fcde
      (void) fflush (stderr);
Packit Service 82fcde
Packit Service 82fcde
      total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
Packit Service 82fcde
      struct abort_msg_s *buf = __mmap (NULL, total, PROT_READ | PROT_WRITE,
Packit Service 82fcde
					MAP_ANON | MAP_PRIVATE, -1, 0);
Packit Service 82fcde
      if (__glibc_likely (buf != MAP_FAILED))
Packit Service 82fcde
	{
Packit Service 82fcde
	  buf->size = total;
Packit Service 82fcde
	  strcpy (buf->msg, str);
Packit Service 82fcde
Packit Service 82fcde
	  /* We have to free the old buffer since the application might
Packit Service 82fcde
	     catch the SIGABRT signal.  */
Packit Service 82fcde
	  struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg, buf);
Packit Service 82fcde
Packit Service 82fcde
	  if (old != NULL)
Packit Service 82fcde
	    __munmap (old, old->size);
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      free (str);
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      /* At least print a minimal message.  */
Packit Service 82fcde
      static const char errstr[] = "Unexpected error.\n";
Packit Service 82fcde
      __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  abort ();
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
#undef __assert_fail
Packit Service 82fcde
void
Packit Service 82fcde
__assert_fail (const char *assertion, const char *file, unsigned int line,
Packit Service 82fcde
	       const char *function)
Packit Service 82fcde
{
Packit Service 82fcde
  __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
Packit Service 82fcde
		      assertion, file, line, function);
Packit Service 82fcde
}
Packit Service 82fcde
hidden_def(__assert_fail)