1 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
29 extern const char *__progname
;
32 #include <libio/iolibio.h>
33 #define fflush(s) _IO_fflush (s)
35 /* This function, when passed a string containing an asserted
36 expression, a filename, and a line number, prints a message
37 on the standard error stream of the form:
38 a.c:10: foobar: Assertion `a == b' failed.
39 It then aborts program execution via a call to `abort'. */
41 #ifdef FATAL_PREPARE_INCLUDE
42 # include FATAL_PREPARE_INCLUDE
47 __assert_fail_base (const char *fmt
, const char *assertion
, const char *file
,
48 unsigned int line
, const char *function
)
57 if (__asprintf (&str
, fmt
,
58 __progname
, __progname
[0] ? ": " : "",
60 function
? function
: "", function
? ": " : "",
61 assertion
, &total
) >= 0)
63 /* Print the message. */
64 (void) __fxprintf (NULL
, "%s", str
);
65 (void) fflush (stderr
);
67 total
= (total
+ 1 + GLRO(dl_pagesize
) - 1) & ~(GLRO(dl_pagesize
) - 1);
68 struct abort_msg_s
*buf
= __mmap (NULL
, total
, PROT_READ
| PROT_WRITE
,
69 MAP_ANON
| MAP_PRIVATE
, -1, 0);
70 if (__glibc_likely (buf
!= MAP_FAILED
))
73 strcpy (buf
->msg
, str
);
75 /* We have to free the old buffer since the application might
76 catch the SIGABRT signal. */
77 struct abort_msg_s
*old
= atomic_exchange_acquire (&__abort_msg
, buf
);
80 __munmap (old
, old
->size
);
87 /* At least print a minimal message. */
88 static const char errstr
[] = "Unexpected error.\n";
89 __libc_write (STDERR_FILENO
, errstr
, sizeof (errstr
) - 1);
98 __assert_fail (const char *assertion
, const char *file
, unsigned int line
,
101 __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
102 assertion
, file
, line
, function
);