From b4570683608a9a349aae20bfa13270cd2b5bee1d Mon Sep 17 00:00:00 2001 From: Rich Salz Date: Thu, 12 Sep 2019 13:06:04 -0400 Subject: [PATCH] ERR: Change get_error_values() to use an enum Reviewed-by: Tomas Mraz Reviewed-by: Richard Levitte (Merged from https://github.com/openssl/openssl/pull/9870) --- crypto/err/err.c | 83 +++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/crypto/err/err.c b/crypto/err/err.c index 51115fd00a..6ad995d679 100644 --- a/crypto/err/err.c +++ b/crypto/err/err.c @@ -131,9 +131,14 @@ static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *); static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL; static int int_err_library_number = ERR_LIB_USER; -static unsigned long get_error_values(int inc, int top, const char **file, - int *line, const char **func, - const char **data, int *flags); +typedef enum ERR_GET_ACTION_e { + EV_POP, EV_PEEK, EV_PEEK_LAST +} ERR_GET_ACTION; + +static unsigned long get_error_values(ERR_GET_ACTION g, + const char **file, int *line, + const char **func, const char **data, + int *flags); static unsigned long err_string_data_hash(const ERR_STRING_DATA *a) { @@ -377,111 +382,112 @@ void ERR_clear_error(void) unsigned long ERR_get_error(void) { - return get_error_values(1, 0, NULL, NULL, NULL, NULL, NULL); + return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL); } unsigned long ERR_get_error_line(const char **file, int *line) { - return get_error_values(1, 0, file, line, NULL, NULL, NULL); + return get_error_values(EV_POP, file, line, NULL, NULL, NULL); } unsigned long ERR_get_error_func(const char **func) { - return get_error_values(1, 0, NULL, NULL, func, NULL, NULL); + return get_error_values(EV_POP, NULL, NULL, func, NULL, NULL); } unsigned long ERR_get_error_data(const char **data, int *flags) { - return get_error_values(1, 0, NULL, NULL, NULL, data, flags); + return get_error_values(EV_POP, NULL, NULL, NULL, data, flags); } unsigned long ERR_get_error_all(const char **file, int *line, const char **func, const char **data, int *flags) { - return get_error_values(1, 0, file, line, func, data, flags); + return get_error_values(EV_POP, file, line, func, data, flags); } #if !OPENSSL_API_3 unsigned long ERR_get_error_line_data(const char **file, int *line, const char **data, int *flags) { - return get_error_values(1, 0, file, line, NULL, data, flags); + return get_error_values(EV_POP, file, line, NULL, data, flags); } #endif unsigned long ERR_peek_error(void) { - return get_error_values(0, 0, NULL, NULL, NULL, NULL, NULL); + return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL); } unsigned long ERR_peek_error_line(const char **file, int *line) { - return get_error_values(0, 0, file, line, NULL, NULL, NULL); + return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL); } unsigned long ERR_peek_error_func(const char **func) { - return get_error_values(0, 0, NULL, NULL, func, NULL, NULL); + return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL); } unsigned long ERR_peek_error_data(const char **data, int *flags) { - return get_error_values(0, 0, NULL, NULL, NULL, data, flags); + return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags); } unsigned long ERR_peek_error_all(const char **file, int *line, const char **func, const char **data, int *flags) { - return get_error_values(0, 0, file, line, func, data, flags); + return get_error_values(EV_PEEK, file, line, func, data, flags); } #if !OPENSSL_API_3 unsigned long ERR_peek_error_line_data(const char **file, int *line, const char **data, int *flags) { - return get_error_values(0, 0, file, line, NULL, data, flags); + return get_error_values(EV_PEEK, file, line, NULL, data, flags); } #endif unsigned long ERR_peek_last_error(void) { - return get_error_values(0, 1, NULL, NULL, NULL, NULL, NULL); + return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL); } unsigned long ERR_peek_last_error_line(const char **file, int *line) { - return get_error_values(0, 1, file, line, NULL, NULL, NULL); + return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL); } unsigned long ERR_peek_last_error_func(const char **func) { - return get_error_values(0, 1, NULL, NULL, func, NULL, NULL); + return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL); } unsigned long ERR_peek_last_error_data(const char **data, int *flags) { - return get_error_values(0, 1, NULL, NULL, NULL, data, flags); + return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags); } unsigned long ERR_peek_last_error_all(const char **file, int *line, const char **func, const char **data, int *flags) { - return get_error_values(0, 1, file, line, func, data, flags); + return get_error_values(EV_PEEK_LAST, file, line, func, data, flags); } #if !OPENSSL_API_3 unsigned long ERR_peek_last_error_line_data(const char **file, int *line, const char **data, int *flags) { - return get_error_values(0, 1, file, line, NULL, data, flags); + return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags); } #endif -static unsigned long get_error_values(int inc, int top, const char **file, - int *line, const char **func, +static unsigned long get_error_values(ERR_GET_ACTION g, + const char **file, int *line, + const char **func, const char **data, int *flags) { int i = 0; @@ -492,21 +498,10 @@ static unsigned long get_error_values(int inc, int top, const char **file, if (es == NULL) return 0; - if (inc && top) { - if (file != NULL) - *file = ""; - if (line != NULL) - *line = 0; - if (func != NULL) - *func = ""; - if (data != NULL) - *data = ""; - if (flags != NULL) - *flags = 0; - - return ERR_R_INTERNAL_ERROR; - } - + /* + * Clear anything that should have been cleared earlier. We do this + * here because this doesn't have constant-time issues. + */ while (es->bottom != es->top) { if (es->err_flags[es->top] & ERR_FLAG_CLEAR) { err_clear(es, es->top, 0); @@ -522,16 +517,18 @@ static unsigned long get_error_values(int inc, int top, const char **file, break; } + /* If everything has been cleared, the stack is empty. */ if (es->bottom == es->top) return 0; - if (top) - i = es->top; /* last error */ + /* Which error, the top of stack (latest one) or the first one? */ + if (g == EV_PEEK_LAST) + i = es->top; else - i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */ + i = (es->bottom + 1) % ERR_NUM_ERRORS; ret = es->err_buffer[i]; - if (inc) { + if (g == EV_POP) { es->bottom = i; es->err_buffer[i] = 0; } @@ -553,7 +550,7 @@ static unsigned long get_error_values(int inc, int top, const char **file, } if (data == NULL) { - if (inc) { + if (g == EV_POP) { err_clear_data(es, i, 0); } } else { -- 2.34.1