Coverage Report

Created: 2026-08-31 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cjose/src/error.c
Line
Count
Source
1
/**
2
 *
3
 * Copyrights
4
 *
5
 * Portions created or assigned to Cisco Systems, Inc. are
6
 * Copyright (c) 2014-2016 Cisco Systems, Inc.  All Rights Reserved.
7
 */
8
9
#include <openssl/err.h>
10
#include "cjose/error.h"
11
12
// thread-local storage specifier: MSVC spells it __declspec(thread),
13
// GCC/Clang use __thread, C11 has _Thread_local
14
#if defined(_MSC_VER)
15
#define CJOSE_THREAD_LOCAL __declspec(thread)
16
#elif defined(__GNUC__) || defined(__clang__)
17
#define CJOSE_THREAD_LOCAL __thread
18
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
19
#define CJOSE_THREAD_LOCAL _Thread_local
20
#else
21
#define CJOSE_THREAD_LOCAL
22
#endif
23
24
////////////////////////////////////////////////////////////////////////////////
25
static const char *_ERR_MSG_TABLE[] = { "no error", "invalid argument", "invalid state", "out of memory", "crypto error" };
26
27
////////////////////////////////////////////////////////////////////////////////
28
const char *cjose_err_message(cjose_errcode code)
29
0
{
30
0
    const char *retval = NULL;
31
0
    if (CJOSE_ERR_CRYPTO == code)
32
0
    {
33
        // for crypto errors, return the most recent openssl error as message;
34
        // render it into a thread-local buffer since ERR_error_string with a
35
        // NULL buffer returns a static buffer shared across threads
36
0
        static CJOSE_THREAD_LOCAL char buf[256];
37
0
        unsigned long err = ERR_get_error();
38
0
        while (0 != err)
39
0
        {
40
0
            ERR_error_string_n(err, buf, sizeof(buf));
41
0
            retval = buf;
42
0
            err = ERR_get_error();
43
0
        }
44
0
    }
45
0
    if (NULL == retval)
46
0
    {
47
        // the code is caller-supplied; don't index the table out of bounds
48
0
        if ((size_t)code >= sizeof(_ERR_MSG_TABLE) / sizeof(_ERR_MSG_TABLE[0]))
49
0
        {
50
0
            return "unknown error";
51
0
        }
52
0
        retval = _ERR_MSG_TABLE[code];
53
0
    }
54
0
    return retval;
55
0
}