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
)
44 if (context
->error_string
)
45 free(context
->error_string
);
46 context
->error_code
= 0;
47 context
->error_string
= NULL
;
51 heim_set_error_message(heim_context context
, heim_error_code ret
,
53 __attribute__ ((__format__ (__printf__
, 3, 4)))
59 heim_vset_error_message(context
, ret
, fmt
, ap
);
64 heim_vset_error_message(heim_context context
, heim_error_code ret
,
65 const char *fmt
, va_list args
)
66 __attribute__ ((__format__ (__printf__
, 3, 0)))
72 if (context
->error_string
) {
73 free(context
->error_string
);
74 context
->error_string
= NULL
;
76 context
->error_code
= ret
;
77 r
= vasprintf(&context
->error_string
, fmt
, args
);
79 context
->error_string
= NULL
;
80 if (context
->error_string
)
81 heim_debug(context
, 200, "error message: %s: %d", context
->error_string
, ret
);
85 heim_prepend_error_message(heim_context context
, heim_error_code ret
,
87 __attribute__ ((__format__ (__printf__
, 3, 4)))
92 heim_vprepend_error_message(context
, ret
, fmt
, ap
);
97 heim_vprepend_error_message(heim_context context
, heim_error_code ret
,
98 const char *fmt
, va_list args
)
99 __attribute__ ((__format__ (__printf__
, 3, 0)))
101 char *str
= NULL
, *str2
= NULL
;
103 if (context
== NULL
|| context
->error_code
!= ret
||
104 vasprintf(&str
, fmt
, args
) < 0 || str
== NULL
)
106 if (context
->error_string
) {
109 e
= asprintf(&str2
, "%s: %s", str
, context
->error_string
);
110 free(context
->error_string
);
111 if (e
< 0 || str2
== NULL
)
112 context
->error_string
= NULL
;
114 context
->error_string
= str2
;
117 context
->error_string
= str
;
121 heim_get_error_message(heim_context context
, heim_error_code code
)
123 const char *cstr
= NULL
;
126 int free_context
= 0;
129 return strdup("Success");
132 * The MIT version of this function ignores the krb5_context
133 * and several widely deployed applications call krb5_get_error_message()
134 * with a NULL context in order to translate an error code as a
135 * replacement for error_message(). Another reason a NULL context
136 * might be provided is if the krb5_init_context() call itself
140 context
->error_string
&&
141 (code
== context
->error_code
|| context
->error_code
== 0) &&
142 (cstr
= strdup(context
->error_string
)))
145 if (context
== NULL
&& (context
= heim_context_init()))
148 cstr
= com_right_r(context
->et_list
, code
, buf
, sizeof(buf
));
150 heim_context_free(&context
);
152 if (cstr
|| (cstr
= error_message(code
)))
154 if (asprintf(&str
, "<unknown error: %d>", (int)code
) == -1 || str
== NULL
)
160 heim_get_error_string(heim_context context
)
162 if (context
&& context
->error_string
)
163 return strdup(context
->error_string
);
168 heim_have_error_string(heim_context context
)
170 return context
&& context
->error_string
!= NULL
;
174 heim_free_error_message(heim_context context
, const char *msg
)
176 free(rk_UNCONST(msg
));