Blame assert/assert.c

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