From 4b7ad8ee4302299917884714f93060d36d6aaf68 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Mon, 17 Jan 2022 14:59:23 -0600 Subject: [PATCH] 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. --- lib/hx509/error.c | 54 +++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/lib/hx509/error.c b/lib/hx509/error.c index 1ca9682d0..aee4f79e7 100644 --- a/lib/hx509/error.c +++ b/lib/hx509/error.c @@ -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, "", 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; }