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 | 461 | { |
30 | 461 | const char *retval = NULL; |
31 | 461 | if (CJOSE_ERR_CRYPTO == code) |
32 | 44 | { |
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 | 44 | static CJOSE_THREAD_LOCAL char buf[256]; |
37 | 44 | unsigned long err = ERR_get_error(); |
38 | 44 | 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 | 44 | } |
45 | 461 | if (NULL == retval) |
46 | 461 | { |
47 | | // the code is caller-supplied; don't index the table out of bounds |
48 | 461 | if ((size_t)code >= sizeof(_ERR_MSG_TABLE) / sizeof(_ERR_MSG_TABLE[0])) |
49 | 0 | { |
50 | 0 | return "unknown error"; |
51 | 0 | } |
52 | 461 | retval = _ERR_MSG_TABLE[code]; |
53 | 461 | } |
54 | 461 | return retval; |
55 | 461 | } |