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_LIB_FUNCTION char * HX509_LIB_CALL
hx509_get_error_string(hx509_context context, int error_code) hx509_get_error_string(hx509_context context, int error_code)
{ {
heim_error_t msg; heim_string_t s = NULL;
heim_string_t s; const char *cstr = NULL;
char *str = NULL; char *str;
if (context == NULL) { if (context) {
const char *sys_err_msg; 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 (cstr == NULL)
if ((sys_err_msg = strerror(error_code))) { cstr = com_right(context->et_list, 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;
}
msg = context->error; if (cstr == NULL && error_code > -1)
if (msg == NULL || heim_error_get_code(msg) != error_code) { cstr = strerror(error_code);
const char *cstr; } /* else this could be an error in hx509_context_init() */
cstr = com_right(context->et_list, error_code); if (cstr == NULL)
if (cstr) cstr = error_message(error_code); /* never returns NULL */
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;
}
s = heim_error_copy_string(msg); str = strdup(cstr);
if (s) { heim_release(s);
const char *cstr = heim_string_get_utf8(s);
if (cstr)
str = strdup(cstr);
heim_release(s);
}
return str; return str;
} }