Skip to content

Commit

Permalink
Avoid ifdefs in trace categories
Browse files Browse the repository at this point in the history
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 <shane.lontis@oracle.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
(Merged from #19917)

(cherry picked from commit 78bd646)
  • Loading branch information
t8m committed Dec 22, 2022
1 parent 0c5fe6e commit e1f0993
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
21 changes: 7 additions & 14 deletions crypto/trace.c
Expand Up @@ -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),
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions include/openssl/trace.h
Expand Up @@ -43,10 +43,8 @@ extern "C" {
# define OSSL_TRACE_CATEGORY_TLS 3
# define OSSL_TRACE_CATEGORY_TLS_CIPHER 4
# define OSSL_TRACE_CATEGORY_CONF 5
# ifndef OPENSSL_NO_ENGINE
# define OSSL_TRACE_CATEGORY_ENGINE_TABLE 6
# define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT 7
# endif
# define OSSL_TRACE_CATEGORY_ENGINE_TABLE 6
# define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT 7
# define OSSL_TRACE_CATEGORY_PKCS5V2 8
# define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN 9
# define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT 10
Expand Down

0 comments on commit e1f0993

Please sign in to comment.