]> git.ipfire.org Git - thirdparty/glibc.git/blame - assert/assert.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / assert / assert.c
CommitLineData
d614a753 1/* Copyright (C) 1991-2020 Free Software Foundation, Inc.
47707456 2 This file is part of the GNU C Library.
28f540f4 3
47707456 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 8
47707456
UD
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
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
28f540f4 17
28f540f4 18#include <assert.h>
48dcd0ba 19#include <atomic.h>
f8a3b5bf 20#include <ldsodefs.h>
51028f34 21#include <libintl.h>
28f540f4
RM
22#include <stdio.h>
23#include <stdlib.h>
24#include <sysdep.h>
383bd1c5 25#include <unistd.h>
f8a3b5bf 26#include <sys/mman.h>
28f540f4
RM
27
28
541587c8 29extern const char *__progname;
28f540f4 30
3ce1f295
UD
31#include <wchar.h>
32#include <libio/iolibio.h>
d18ea0c5 33#define fflush(s) _IO_fflush (s)
50304ef0 34
28f540f4
RM
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:
f8a3b5bf 38 a.c:10: foobar: Assertion `a == b' failed.
28f540f4
RM
39 It then aborts program execution via a call to `abort'. */
40
41#ifdef FATAL_PREPARE_INCLUDE
50304ef0 42# include FATAL_PREPARE_INCLUDE
28f540f4
RM
43#endif
44
48dcd0ba 45
28f540f4 46void
f8a3b5bf
UD
47__assert_fail_base (const char *fmt, const char *assertion, const char *file,
48 unsigned int line, const char *function)
28f540f4 49{
f8a3b5bf 50 char *str;
51028f34 51
28f540f4
RM
52#ifdef FATAL_PREPARE
53 FATAL_PREPARE;
54#endif
55
f8a3b5bf
UD
56 int total;
57 if (__asprintf (&str, fmt,
383bd1c5
UD
58 __progname, __progname[0] ? ": " : "",
59 file, line,
60 function ? function : "", function ? ": " : "",
f8a3b5bf 61 assertion, &total) >= 0)
383bd1c5
UD
62 {
63 /* Print the message. */
f8a3b5bf 64 (void) __fxprintf (NULL, "%s", str);
383bd1c5 65 (void) fflush (stderr);
28f540f4 66
f8a3b5bf
UD
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);
a1ffb40e 70 if (__glibc_likely (buf != MAP_FAILED))
f8a3b5bf
UD
71 {
72 buf->size = total;
73 strcpy (buf->msg, str);
74
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_acq (&__abort_msg, buf);
78
79 if (old != NULL)
80 __munmap (old, old->size);
81 }
82
83 free (str);
383bd1c5
UD
84 }
85 else
fab656f5
UD
86 {
87 /* At least print a minimal message. */
88 static const char errstr[] = "Unexpected error.\n";
89 __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
90 }
51028f34 91
28f540f4
RM
92 abort ();
93}
f8a3b5bf
UD
94
95
96#undef __assert_fail
97void
98__assert_fail (const char *assertion, const char *file, unsigned int line,
99 const char *function)
100{
101 __assert_fail_base (_("%s%s%s:%u: %s%sAssertion `%s' failed.\n%n"),
102 assertion, file, line, function);
103}
7abb683a 104hidden_def(__assert_fail)