From e1f09938ca69b080467112efa3111213042ff8db Mon Sep 17 00:00:00 2001 From: Tomas Mraz Date: Thu, 15 Dec 2022 11:45:48 +0100 Subject: Avoid ifdefs in trace categories The trace code assumes all categories are present and the category numbers are equal to the index in the table. Fixes #19915 Reviewed-by: Shane Lontis Reviewed-by: Dmitry Belyavskiy (Merged from https://github.com/openssl/openssl/pull/19917) (cherry picked from commit 78bd646b2f6a18cf8515e05a5f3efadff03b3920) --- crypto/trace.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'crypto') diff --git a/crypto/trace.c b/crypto/trace.c index 0d58932825..74dbd525a5 100644 --- a/crypto/trace.c +++ b/crypto/trace.c @@ -118,17 +118,16 @@ struct trace_category_st { }; #define TRACE_CATEGORY_(name) { #name, OSSL_TRACE_CATEGORY_##name } -static const struct trace_category_st trace_categories[] = { +static const struct trace_category_st + trace_categories[OSSL_TRACE_CATEGORY_NUM] = { TRACE_CATEGORY_(ALL), TRACE_CATEGORY_(TRACE), TRACE_CATEGORY_(INIT), TRACE_CATEGORY_(TLS), TRACE_CATEGORY_(TLS_CIPHER), TRACE_CATEGORY_(CONF), -#ifndef OPENSSL_NO_ENGINE TRACE_CATEGORY_(ENGINE_TABLE), TRACE_CATEGORY_(ENGINE_REF_COUNT), -#endif TRACE_CATEGORY_(PKCS5V2), TRACE_CATEGORY_(PKCS12_KEYGEN), TRACE_CATEGORY_(PKCS12_DECRYPT), @@ -144,22 +143,16 @@ static const struct trace_category_st trace_categories[] = { const char *OSSL_trace_get_category_name(int num) { - size_t i; - + if (num < 0 || (size_t)num >= OSSL_NELEM(trace_categories)) + return NULL; /* * Partial check that OSSL_TRACE_CATEGORY_... macros * are synced with trace_categories array */ -#ifndef OPENSSL_NO_ENGINE - if (!ossl_assert(OSSL_TRACE_CATEGORY_NUM == OSSL_NELEM(trace_categories))) + if (!ossl_assert(trace_categories[num].name != NULL) + || !ossl_assert(trace_categories[num].num == num)) return NULL; -#endif - - for (i = 0; i < OSSL_NELEM(trace_categories); i++) - if (trace_categories[i].num == num) - return trace_categories[i].name; - - return NULL; /* not found */ + return trace_categories[num].name; } int OSSL_trace_get_category_num(const char *name) -- cgit v1.2.3