1/* Copyright (C) 1991-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
17
18#include <assert.h>
19#include <atomic.h>
20#include <ldsodefs.h>
21#include <libintl.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <sysdep.h>
25#include <unistd.h>
26#include <sys/mman.h>
27#include <setvmaname.h>
28
29
30extern const char *__progname;
31
32#include <wchar.h>
33#include <libio/iolibio.h>
34#define fflush(s) _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
47void
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, fmt: "%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 (__glibc_likely (buf != MAP_FAILED))
72 {
73 buf->size = total;
74 strcpy (buf->msg, str);
75 __set_vma_name (start: buf, len: total, name: " glibc: assert");
76
77 /* We have to free the old buffer since the application might
78 catch the SIGABRT signal. */
79 struct abort_msg_s *old = atomic_exchange_acquire (&__abort_msg, buf);
80
81 if (old != NULL)
82 __munmap (old, old->size);
83 }
84
85 free (ptr: str);
86 }
87 else
88 {
89 /* At least print a minimal message. */
90 static const char errstr[] = "Unexpected error.\n";
91 __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
92 }
93
94 abort ();
95}
96
97
98#undef __assert_fail
99void
100__assert_fail (const char *assertion, const char *file, unsigned int line,
101 const char *function)
102{
103 __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
104 assertion, file, line, function);
105}
106

source code of glibc/assert/assert.c