hx509: Fix strerror(negative); streamline code

Calling strerror() with a negative value is an error.

Besides fixing that, we streamline hx509_get_error_string(), making it much
simpler and easier to read.
This commit is contained in:
Nicolas Williams
2022-01-17 14:59:23 -06:00
parent 7cfab00e37
commit 4b7ad8ee43

View File

@@ -147,48 +147,28 @@ hx509_enomem(hx509_context context)
HX509_LIB_FUNCTION char * HX509_LIB_CALL
hx509_get_error_string(hx509_context context, int error_code)
{
heim_error_t msg;
heim_string_t s;
char *str = NULL;
heim_string_t s = NULL;
const char *cstr = NULL;
char *str;
if (context == NULL) {
const char *sys_err_msg;
if (context) {
if (context->error &&
heim_error_get_code(context->error) == error_code &&
(s = heim_error_copy_string(context->error)))
cstr = heim_string_get_utf8(s);
/* This case should only happen on hx509_context_init() failure */
if ((sys_err_msg = strerror(error_code))) {
if (asprintf(&str, "hx509_context_init system error: %s (%d)",
sys_err_msg, error_code) == -1)
return NULL;
return str;
}
if (asprintf(&str, "hx509_context_init unknown error: %d",
error_code) == -1)
return NULL;
return str;
}
if (cstr == NULL)
cstr = com_right(context->et_list, error_code);
msg = context->error;
if (msg == NULL || heim_error_get_code(msg) != error_code) {
const char *cstr;
if (cstr == NULL && error_code > -1)
cstr = strerror(error_code);
} /* else this could be an error in hx509_context_init() */
cstr = com_right(context->et_list, error_code);
if (cstr)
return strdup(cstr);
cstr = strerror(error_code);
if (cstr)
return strdup(cstr);
if (asprintf(&str, "<unknown error: %d>", error_code) == -1)
return NULL;
return str;
}
if (cstr == NULL)
cstr = error_message(error_code); /* never returns NULL */
s = heim_error_copy_string(msg);
if (s) {
const char *cstr = heim_string_get_utf8(s);
if (cstr)
str = strdup(cstr);
heim_release(s);
}
str = strdup(cstr);
heim_release(s);
return str;
}