Replace FSF snail mail address with URLs.
[platform/upstream/glibc.git] / assert / assert.c
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.
4
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.
9
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.
14
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/>.  */
18
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>
28
29
30 extern const char *__progname;
31
32 #include <wchar.h>
33 #include <libio/iolibio.h>
34 #define fflush(s) INTUSE(_IO_fflush) (s)
35
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'.  */
41
42 #ifdef FATAL_PREPARE_INCLUDE
43 # include FATAL_PREPARE_INCLUDE
44 #endif
45
46
47 void
48 __assert_fail_base (const char *fmt, const char *assertion, const char *file,
49                     unsigned int line, const char *function)
50 {
51   char *str;
52
53 #ifdef FATAL_PREPARE
54   FATAL_PREPARE;
55 #endif
56
57   int total;
58   if (__asprintf (&str, fmt,
59                   __progname, __progname[0] ? ": " : "",
60                   file, line,
61                   function ? function : "", function ? ": " : "",
62                   assertion, &total) >= 0)
63     {
64       /* Print the message.  */
65       (void) __fxprintf (NULL, "%s", str);
66       (void) fflush (stderr);
67
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))
72         {
73           buf->size = total;
74           strcpy (buf->msg, str);
75
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);
79
80           if (old != NULL)
81             __munmap (old, old->size);
82         }
83
84       free (str);
85     }
86   else
87     {
88       /* At least print a minimal message.  */
89       static const char errstr[] = "Unexpected error.\n";
90       __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
91     }
92
93   abort ();
94 }
95
96
97 #undef __assert_fail
98 void
99 __assert_fail (const char *assertion, const char *file, unsigned int line,
100                const char *function)
101 {
102   __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
103                       assertion, file, line, function);
104 }
105 hidden_def(__assert_fail)