catch error from as.*printf
[heimdal.git] / lib / krb5 / error_string.c
blob64738fc68aa67634f37f1c1b6e8f0d4eb8eed45b
1 /*
2 * Copyright (c) 2001, 2003, 2005 - 2006 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 #include "krb5_locl.h"
36 #undef __attribute__
37 #define __attribute__(x)
39 /**
40 * Clears the error message from the Kerberos 5 context.
42 * @param context The Kerberos 5 context to clear
44 * @ingroup krb5_error
47 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
48 krb5_clear_error_message(krb5_context context)
50 HEIMDAL_MUTEX_lock(context->mutex);
51 if (context->error_string)
52 free(context->error_string);
53 context->error_code = 0;
54 context->error_string = NULL;
55 HEIMDAL_MUTEX_unlock(context->mutex);
58 /**
59 * Set the context full error string for a specific error code.
60 * The error that is stored should be internationalized.
62 * @param context Kerberos 5 context
63 * @param ret The error code
64 * @param fmt Error string for the error code
65 * @param ... printf(3) style parameters.
67 * @ingroup krb5_error
70 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
71 krb5_set_error_message(krb5_context context, krb5_error_code ret,
72 const char *fmt, ...)
73 __attribute__ ((format (printf, 3, 4)))
75 va_list ap;
77 va_start(ap, fmt);
78 krb5_vset_error_message (context, ret, fmt, ap);
79 va_end(ap);
82 /**
83 * Set the context full error string for a specific error code.
85 * @param context Kerberos 5 context
86 * @param ret The error code
87 * @param fmt Error string for the error code
88 * @param args printf(3) style parameters.
90 * @ingroup krb5_error
94 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
95 krb5_vset_error_message (krb5_context context, krb5_error_code ret,
96 const char *fmt, va_list args)
97 __attribute__ ((format (printf, 3, 0)))
99 int r;
101 krb5_clear_error_message(context);
102 HEIMDAL_MUTEX_lock(context->mutex);
103 context->error_code = ret;
104 r = vasprintf(&context->error_string, fmt, args);
105 if (r)
106 context->error_string = NULL;
107 HEIMDAL_MUTEX_unlock(context->mutex);
111 * Prepend the context full error string for a specific error code.
112 * The error that is stored should be internationalized.
114 * @param context Kerberos 5 context
115 * @param ret The error code
116 * @param fmt Error string for the error code
117 * @param ... printf(3) style parameters.
119 * @ingroup krb5_error
122 void KRB5_LIB_FUNCTION
123 krb5_prepend_error_message(krb5_context context, krb5_error_code ret,
124 const char *fmt, ...)
125 __attribute__ ((format (printf, 3, 4)))
127 va_list ap;
129 va_start(ap, fmt);
130 krb5_vprepend_error_message(context, ret, fmt, ap);
131 va_end(ap);
135 * Prepend the contexts's full error string for a specific error code.
137 * @param context Kerberos 5 context
138 * @param ret The error code
139 * @param fmt Error string for the error code
140 * @param args printf(3) style parameters.
142 * @ingroup krb5_error
145 void KRB5_LIB_FUNCTION
146 krb5_vprepend_error_message(krb5_context context, krb5_error_code ret,
147 const char *fmt, va_list args)
148 __attribute__ ((format (printf, 3, 0)))
150 char *str = NULL, *str2 = NULL;
151 HEIMDAL_MUTEX_lock(context->mutex);
152 if (context->error_code != ret) {
153 HEIMDAL_MUTEX_unlock(context->mutex);
154 return;
156 if (vasprintf(&str, fmt, args) < 0 || str == NULL) {
157 HEIMDAL_MUTEX_unlock(context->mutex);
158 return;
160 if (context->error_string) {
161 int e;
163 e = asprintf(&str2, "%s: %s", str, context->error_string);
164 free(context->error_string);
165 if (e < 0 || str2 == NULL)
166 context->error_string = NULL;
167 else
168 context->error_string = str2;
169 free(str);
170 } else
171 context->error_string = str;
172 HEIMDAL_MUTEX_unlock(context->mutex);
177 * Return the error message in context. On error or no error string,
178 * the function returns NULL.
180 * @param context Kerberos 5 context
182 * @return an error string, needs to be freed with
183 * krb5_free_error_message(). The functions return NULL on error.
185 * @ingroup krb5_error
188 KRB5_LIB_FUNCTION char * KRB5_LIB_CALL
189 krb5_get_error_string(krb5_context context)
191 char *ret = NULL;
193 HEIMDAL_MUTEX_lock(context->mutex);
194 if (context->error_string)
195 ret = strdup(context->error_string);
196 HEIMDAL_MUTEX_unlock(context->mutex);
197 return ret;
200 KRB5_LIB_FUNCTION krb5_boolean KRB5_LIB_CALL
201 krb5_have_error_string(krb5_context context)
203 char *str;
204 HEIMDAL_MUTEX_lock(context->mutex);
205 str = context->error_string;
206 HEIMDAL_MUTEX_unlock(context->mutex);
207 return str != NULL;
211 * Return the error message for `code' in context. On memory
212 * allocation error the function returns NULL.
214 * @param context Kerberos 5 context
215 * @param code Error code related to the error
217 * @return an error string, needs to be freed with
218 * krb5_free_error_message(). The functions return NULL on error.
220 * @ingroup krb5_error
223 KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL
224 krb5_get_error_message(krb5_context context, krb5_error_code code)
226 char *str;
228 HEIMDAL_MUTEX_lock(context->mutex);
229 if (context->error_string &&
230 (code == context->error_code || context->error_code == 0))
232 str = strdup(context->error_string);
233 if (str) {
234 HEIMDAL_MUTEX_unlock(context->mutex);
235 return str;
238 HEIMDAL_MUTEX_unlock(context->mutex);
240 if (code == 0)
241 return strdup("Success");
243 const char *msg;
244 char buf[128];
245 msg = com_right_r(context->et_list, code, buf, sizeof(buf));
246 if (msg)
247 return strdup(msg);
250 if (asprintf(&str, "<unknown error: %d>", (int)code) == -1 || str == NULL)
251 return NULL;
253 return str;
258 * Free the error message returned by krb5_get_error_message().
260 * @param context Kerberos context
261 * @param msg error message to free, returned byg
262 * krb5_get_error_message().
264 * @ingroup krb5_error
267 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
268 krb5_free_error_message(krb5_context context, const char *msg)
270 free(rk_UNCONST(msg));
275 * Return the error string for the error code. The caller must not
276 * free the string.
278 * This function is deprecated since its not threadsafe.
280 * @param context Kerberos 5 context.
281 * @param code Kerberos error code.
283 * @return the error message matching code
285 * @ingroup krb5
288 KRB5_DEPRECATED
289 KRB5_LIB_FUNCTION const char* KRB5_LIB_CALL
290 krb5_get_err_text(krb5_context context, krb5_error_code code)
292 const char *p = NULL;
293 if(context != NULL)
294 p = com_right(context->et_list, code);
295 if(p == NULL)
296 p = strerror(code);
297 if (p == NULL)
298 p = "Unknown error";
299 return p;