2 * Copyright (c) 2001, 2003, 2005 - 2020 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
37 #define __attribute__(x)
40 heim_clear_error_message(heim_context context
)
42 if (context
->error_string
)
43 free(context
->error_string
);
44 context
->error_code
= 0;
45 context
->error_string
= NULL
;
49 heim_set_error_message(heim_context context
, heim_error_code ret
,
51 __attribute__ ((__format__ (__printf__
, 3, 4)))
56 heim_vset_error_message(context
, ret
, fmt
, ap
);
61 heim_vset_error_message(heim_context context
, heim_error_code ret
,
62 const char *fmt
, va_list args
)
63 __attribute__ ((__format__ (__printf__
, 3, 0)))
69 if (context
->error_string
) {
70 free(context
->error_string
);
71 context
->error_string
= NULL
;
73 context
->error_code
= ret
;
74 r
= vasprintf(&context
->error_string
, fmt
, args
);
76 context
->error_string
= NULL
;
77 if (context
->error_string
)
78 heim_debug(context
, 200, "error message: %s: %d", context
->error_string
, ret
);
82 heim_prepend_error_message(heim_context context
, heim_error_code ret
,
84 __attribute__ ((__format__ (__printf__
, 3, 4)))
89 heim_vprepend_error_message(context
, ret
, fmt
, ap
);
94 heim_vprepend_error_message(heim_context context
, heim_error_code ret
,
95 const char *fmt
, va_list args
)
96 __attribute__ ((__format__ (__printf__
, 3, 0)))
98 char *str
= NULL
, *str2
= NULL
;
100 if (context
== NULL
|| context
->error_code
!= ret
||
101 vasprintf(&str
, fmt
, args
) < 0 || str
== NULL
)
103 if (context
->error_string
) {
106 e
= asprintf(&str2
, "%s: %s", str
, context
->error_string
);
107 free(context
->error_string
);
108 if (e
< 0 || str2
== NULL
)
109 context
->error_string
= NULL
;
111 context
->error_string
= str2
;
114 context
->error_string
= str
;
118 heim_get_error_message(heim_context context
, heim_error_code code
)
120 const char *cstr
= NULL
;
123 int free_context
= 0;
126 return strdup("Success");
129 * The MIT version of this function ignores the krb5_context
130 * and several widely deployed applications call krb5_get_error_message()
131 * with a NULL context in order to translate an error code as a
132 * replacement for error_message(). Another reason a NULL context
133 * might be provided is if the krb5_init_context() call itself
137 context
->error_string
&&
138 (code
== context
->error_code
|| context
->error_code
== 0) &&
139 (cstr
= strdup(context
->error_string
)))
142 if (context
== NULL
&& (context
= heim_context_init()))
145 cstr
= com_right_r(context
->et_list
, code
, buf
, sizeof(buf
));
147 heim_context_free(&context
);
149 if (cstr
|| (cstr
= error_message(code
)))
151 if (asprintf(&str
, "<unknown error: %d>", (int)code
) == -1 || str
== NULL
)
157 heim_get_error_string(heim_context context
)
159 if (context
&& context
->error_string
)
160 return strdup(context
->error_string
);
165 heim_have_error_string(heim_context context
)
167 return context
->error_string
!= NULL
;
171 heim_free_error_message(heim_context context
, const char *msg
)
173 free(rk_UNCONST(msg
));