xref: /freebsd/share/man/man3/assert.3 (revision 4c9a0adad18263ec8725d9bfc5f560c6ad1da8bd)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)assert.3	8.1 (Berkeley) 6/9/93
29.\"
30.Dd April 20, 2021
31.Dt ASSERT 3
32.Os
33.Sh NAME
34.Nm assert ,
35.Nm static_assert
36.Nd expression verification macro
37.Sh SYNOPSIS
38.In assert.h
39.Fn assert expression
40.Fn static_assert expression
41.Fn static_assert expression message
42.Sh DESCRIPTION
43The
44.Fn assert
45macro tests the given
46.Ar expression
47and if it is false,
48the calling process is terminated.
49A diagnostic message is written to
50.Dv stderr
51and the function
52.Xr abort 3
53is called, effectively terminating the program.
54.Pp
55If
56.Ar expression
57is true,
58the
59.Fn assert
60macro does nothing.
61.Pp
62The
63.Fn assert
64macro
65may be removed at compile time by defining
66.Dv NDEBUG
67as a macro
68(e.g., by using the
69.Xr cc 1
70option
71.Fl D Ns Dv NDEBUG ) .
72Unlike most other include files,
73.In assert.h
74may be included multiple times.
75Each time whether or not
76.Dv NDEBUG
77is defined determines the behavior of assert from that point forward
78until the end of the unit or another include of
79.In assert.h .
80.Pp
81The
82.Fn assert
83macro should only be used for ensuring the developer's expectations
84hold true.
85It is not appropriate for regular run-time error detection.
86.Pp
87The
88.Fn static_assert
89macro expands to
90.Fn _Static_assert ,
91and, contrarily to
92.Fn assert ,
93makes assertions at compile-time.
94Once the constraint is violated, the compiler produces a diagnostic
95message including the string literal message, if provided.
96The initial form of the
97.Fn _Static_assert
98containing a string literal message was introduced in C11 standard, and
99the other form with no string literal is to be implemented by C2x and
100some compilers may lack its adoption at present.
101.Sh EXAMPLES
102The assertion:
103.Dl "assert(1 == 0);"
104generates a diagnostic message similar to the following:
105.Dl "Assertion failed: (1 == 0), function main, file main.c, line 100."
106.Pp
107The following assert tries to assert there was no partial read:
108.Dl "assert(read(fd, buf, nbytes) == nbytes);"
109However, there are two problems.
110First, it checks for normal conditions, rather than conditions that
111indicate a bug.
112Second, the code will disappear if
113.Dv NDEBUG
114is defined, changing the semantics of the program.
115.Pp
116The following asserts that the size of the S structure is 16.
117Otherwise, it produces a diagnostic message which points at the
118constraint and includes the provided string literal:
119.Dl "static_assert(sizeof(struct S) == 16, ""size mismatch"");"
120If none is provided, it only points at the constraint.
121.Sh SEE ALSO
122.Xr abort2 2 ,
123.Xr abort 3
124.Sh STANDARDS
125.Rs
126The
127.Fn assert
128macro conforms to
129.St -isoC-99 .
130.Re
131.Pp
132.Rs
133The
134.Fn static_assert
135macro conforms to
136.St -isoC-2011 .
137.Re
138.Sh HISTORY
139An
140.Nm
141macro appeared in
142.At v7 .
143