ERR: Change get_error_values() to use an enum

Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9870)
This commit is contained in:
Rich Salz 2019-09-12 13:06:04 -04:00 committed by Richard Levitte
parent f28bc7d386
commit b457068360
1 changed files with 40 additions and 43 deletions

View File

@ -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 LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
static int int_err_library_number = ERR_LIB_USER; static int int_err_library_number = ERR_LIB_USER;
static unsigned long get_error_values(int inc, int top, const char **file, typedef enum ERR_GET_ACTION_e {
int *line, const char **func, EV_POP, EV_PEEK, EV_PEEK_LAST
const char **data, int *flags); } 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) 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) 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) 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) 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) 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, unsigned long ERR_get_error_all(const char **file, int *line,
const char **func, const char **func,
const char **data, int *flags) 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 #if !OPENSSL_API_3
unsigned long ERR_get_error_line_data(const char **file, int *line, unsigned long ERR_get_error_line_data(const char **file, int *line,
const char **data, int *flags) 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 #endif
unsigned long ERR_peek_error(void) 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) 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) 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) 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, unsigned long ERR_peek_error_all(const char **file, int *line,
const char **func, const char **func,
const char **data, int *flags) 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 #if !OPENSSL_API_3
unsigned long ERR_peek_error_line_data(const char **file, int *line, unsigned long ERR_peek_error_line_data(const char **file, int *line,
const char **data, int *flags) 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 #endif
unsigned long ERR_peek_last_error(void) 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) 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) 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) 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, unsigned long ERR_peek_last_error_all(const char **file, int *line,
const char **func, const char **func,
const char **data, int *flags) 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 #if !OPENSSL_API_3
unsigned long ERR_peek_last_error_line_data(const char **file, int *line, unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
const char **data, int *flags) 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 #endif
static unsigned long get_error_values(int inc, int top, const char **file, static unsigned long get_error_values(ERR_GET_ACTION g,
int *line, const char **func, const char **file, int *line,
const char **func,
const char **data, int *flags) const char **data, int *flags)
{ {
int i = 0; int i = 0;
@ -492,21 +498,10 @@ static unsigned long get_error_values(int inc, int top, const char **file,
if (es == NULL) if (es == NULL)
return 0; return 0;
if (inc && top) { /*
if (file != NULL) * Clear anything that should have been cleared earlier. We do this
*file = ""; * here because this doesn't have constant-time issues.
if (line != NULL) */
*line = 0;
if (func != NULL)
*func = "";
if (data != NULL)
*data = "";
if (flags != NULL)
*flags = 0;
return ERR_R_INTERNAL_ERROR;
}
while (es->bottom != es->top) { while (es->bottom != es->top) {
if (es->err_flags[es->top] & ERR_FLAG_CLEAR) { if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
err_clear(es, es->top, 0); err_clear(es, es->top, 0);
@ -522,16 +517,18 @@ static unsigned long get_error_values(int inc, int top, const char **file,
break; break;
} }
/* If everything has been cleared, the stack is empty. */
if (es->bottom == es->top) if (es->bottom == es->top)
return 0; return 0;
if (top) /* Which error, the top of stack (latest one) or the first one? */
i = es->top; /* last error */ if (g == EV_PEEK_LAST)
i = es->top;
else else
i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */ i = (es->bottom + 1) % ERR_NUM_ERRORS;
ret = es->err_buffer[i]; ret = es->err_buffer[i];
if (inc) { if (g == EV_POP) {
es->bottom = i; es->bottom = i;
es->err_buffer[i] = 0; 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 (data == NULL) {
if (inc) { if (g == EV_POP) {
err_clear_data(es, i, 0); err_clear_data(es, i, 0);
} }
} else { } else {