hx509: Fix coverity warnings
[heimdal.git] / lib / base / warn.c
blob2376a518ee7f287775fd6d7a93ae4457c33288dd
1 /*
2 * Copyright (c) 1997 - 2020 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #if defined(_MSC_VER)
35 # pragma warning(disable: 4646)
36 # pragma warning(disable: 4716)
37 #endif
39 #include "baselocl.h"
40 #include <err.h>
42 static heim_error_code _warnerr(heim_context context, int do_errtext,
43 heim_error_code code, int level, const char *fmt, va_list ap)
44 __attribute__ ((__format__ (__printf__, 5, 0)));
46 static heim_error_code
47 _warnerr(heim_context context, int do_errtext,
48 heim_error_code code, int level, const char *fmt, va_list ap)
50 char xfmt[7] = "";
51 const char *args[2], **arg;
52 char *msg = NULL;
53 const char *err_str = NULL;
54 heim_error_code ret;
56 args[0] = args[1] = NULL;
57 arg = args;
58 if(fmt){
59 strlcat(xfmt, "%s", sizeof(xfmt));
60 if(do_errtext)
61 strlcat(xfmt, ": ", sizeof(xfmt));
62 ret = vasprintf(&msg, fmt, ap);
63 if(ret < 0 || msg == NULL)
64 return ENOMEM;
65 *arg++ = msg;
67 if (context && do_errtext) {
68 strlcat(xfmt, "%s", sizeof(xfmt));
70 err_str = heim_get_error_message(context, code);
71 if (err_str != NULL) {
72 *arg = err_str;
73 } else {
74 *arg= "<unknown error>";
78 if (context && heim_get_warn_dest(context))
79 heim_log(context, heim_get_warn_dest(context), level, xfmt, args[0],
80 args[1]);
81 else
82 warnx(xfmt, args[0], args[1]);
83 free(msg);
84 heim_free_error_message(context, err_str);
85 return 0;
88 #define FUNC(ETEXT, CODE, LEVEL) \
89 heim_error_code ret; \
90 va_list ap; \
91 va_start(ap, fmt); \
92 ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); \
93 va_end(ap);
95 #undef __attribute__
96 #define __attribute__(X)
98 /**
99 * Log a warning to the log, default stderr, include the error from
100 * the last failure.
102 * @param context A Kerberos 5 context.
103 * @param code error code of the last error
104 * @param fmt message to print
105 * @param ap arguments
107 * @ingroup heim_error
110 heim_error_code
111 heim_vwarn(heim_context context, heim_error_code code,
112 const char *fmt, va_list ap)
113 __attribute__ ((__format__ (__printf__, 3, 0)))
115 return _warnerr(context, 1, code, 1, fmt, ap);
119 * Log a warning to the log, default stderr, include the error from
120 * the last failure.
122 * @param context A Kerberos 5 context.
123 * @param code error code of the last error
124 * @param fmt message to print
126 * @ingroup heim_error
129 heim_error_code
130 heim_warn(heim_context context, heim_error_code code, const char *fmt, ...)
131 __attribute__ ((__format__ (__printf__, 3, 4)))
133 FUNC(1, code, 1);
134 return ret;
138 * Log a warning to the log, default stderr.
140 * @param context A Kerberos 5 context.
141 * @param fmt message to print
142 * @param ap arguments
144 * @ingroup heim_error
147 heim_error_code
148 heim_vwarnx(heim_context context, const char *fmt, va_list ap)
149 __attribute__ ((__format__ (__printf__, 2, 0)))
151 return _warnerr(context, 0, 0, 1, fmt, ap);
155 * Log a warning to the log, default stderr.
157 * @param context A Kerberos 5 context.
158 * @param fmt message to print
160 * @ingroup heim_error
163 heim_error_code
164 heim_warnx(heim_context context, const char *fmt, ...)
165 __attribute__ ((__format__ (__printf__, 2, 3)))
167 FUNC(0, 0, 1);
168 return ret;