Correct incorrect bug number ordering in resolved bug list.
[glibc.git] / assert / assert.c
blob0830ac0389ea2ac6aacf374ac43907f1b60559be
1 /* Copyright (C) 1991,1994-1996,1998,2001,2002,2005,2009,2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <atomic.h>
21 #include <ldsodefs.h>
22 #include <libintl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sysdep.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
30 extern const char *__progname;
32 #include <wchar.h>
33 #include <libio/iolibio.h>
34 #define fflush(s) INTUSE(_IO_fflush) (s)
36 /* This function, when passed a string containing an asserted
37 expression, a filename, and a line number, prints a message
38 on the standard error stream of the form:
39 a.c:10: foobar: Assertion `a == b' failed.
40 It then aborts program execution via a call to `abort'. */
42 #ifdef FATAL_PREPARE_INCLUDE
43 # include FATAL_PREPARE_INCLUDE
44 #endif
47 void
48 __assert_fail_base (const char *fmt, const char *assertion, const char *file,
49 unsigned int line, const char *function)
51 char *str;
53 #ifdef FATAL_PREPARE
54 FATAL_PREPARE;
55 #endif
57 int total;
58 if (__asprintf (&str, fmt,
59 __progname, __progname[0] ? ": " : "",
60 file, line,
61 function ? function : "", function ? ": " : "",
62 assertion, &total) >= 0)
64 /* Print the message. */
65 (void) __fxprintf (NULL, "%s", str);
66 (void) fflush (stderr);
68 total = (total + 1 + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
69 struct abort_msg_s *buf = __mmap (NULL, total, PROT_READ | PROT_WRITE,
70 MAP_ANON | MAP_PRIVATE, -1, 0);
71 if (__builtin_expect (buf != MAP_FAILED, 1))
73 buf->size = total;
74 strcpy (buf->msg, str);
76 /* We have to free the old buffer since the application might
77 catch the SIGABRT signal. */
78 struct abort_msg_s *old = atomic_exchange_acq (&__abort_msg, buf);
80 if (old != NULL)
81 __munmap (old, old->size);
84 free (str);
86 else
88 /* At least print a minimal message. */
89 static const char errstr[] = "Unexpected error.\n";
90 __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
93 abort ();
97 #undef __assert_fail
98 void
99 __assert_fail (const char *assertion, const char *file, unsigned int line,
100 const char *function)
102 __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
103 assertion, file, line, function);
105 hidden_def(__assert_fail)