1 .\" $OpenBSD: err.3,v 1.20 2014/04/23 16:26:33 jmc Exp $
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 .Nd formatted error messages
50 .Fn err "int eval" "const char *fmt" "..."
52 .Fn verr "int eval" "const char *fmt" "va_list args"
54 .Fn errc "int eval" "int code" "const char *fmt" "..."
56 .Fn verrc "int eval" "int code" "const char *fmt" "va_list args"
58 .Fn errx "int eval" "const char *fmt" "..."
60 .Fn verrx "int eval" "const char *fmt" "va_list args"
62 .Fn warn "const char *fmt" "..."
64 .Fn vwarn "const char *fmt" "va_list args"
66 .Fn warnc "int code" "const char *fmt" "..."
68 .Fn vwarnc "int code" "const char *fmt" "va_list args"
70 .Fn warnx "const char *fmt" "..."
72 .Fn vwarnx "const char *fmt" "va_list args"
78 family of functions display a formatted error message on the standard
80 In all cases, the last component of the program name, followed by
83 character and a space, are output.
84 The text that follows depends on the function being called.
87 specification (and associated arguments) may be any format allowed by
95 the formatted error message is output.
103 functions only, no additional text is output,
104 so the output looks like the following:
105 .Bd -literal -offset indent
109 The other functions all output an error message string affiliated with
112 preceded by a colon character and a space if
116 That is, the output is as follows:
117 .Bd -literal -offset indent
118 progname: fmt: error message string
126 .Bd -literal -offset indent
127 progname: error message string
138 functions, the error value used is the current value of the global variable
146 function the argument
150 In all cases, the output is followed by a newline character.
160 functions do not return, but exit with the value of the argument
165 information string and exit:
166 .Bd -literal -offset indent
167 if ((p = malloc(size)) == NULL)
169 if ((fd = open(file_name, O_RDONLY, 0)) == -1)
170 err(1, "%s", file_name);
173 Display an error message and exit:
174 .Bd -literal -offset indent
175 if (tm.tm_hour < START_TIME)
176 errx(1, "too early, wait until %s", start_time_string);
180 .Bd -literal -offset indent
181 if ((fd = open(raw_device, O_RDONLY, 0)) == -1)
182 warnx("%s: %s: trying the block device",
183 raw_device, strerror(errno));
184 if ((fd = open(block_device, O_RDONLY, 0)) == -1)
185 err(1, "%s", block_device);
214 It is important never to pass a string with user-supplied data as a
217 An attacker can put format specifiers in the string to mangle the stack,
218 leading to a possible security hole.
219 This holds true even if the string has been built
221 using a function like
223 as the resulting string may still contain user-supplied conversion specifiers
224 for later interpolation by the
230 Always be sure to use the proper secure idiom:
231 .Bd -literal -offset indent
232 err(1, "%s", string);