Skip to content

Commit e487f84

Browse files
committed
Introduce CRYPT_ERR_NUM and build-time check err_2_str[] size
Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 507c668 commit e487f84

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/headers/tomcrypt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ enum {
7575
CRYPT_HASH_OVERFLOW, /* Hash applied to too many bits */
7676
CRYPT_PW_CTX_MISSING, /* Password context to decrypt key file is missing */
7777
CRYPT_UNKNOWN_PEM, /* The PEM header was not recognized */
78+
79+
/* Here only follows the number of error codes.
80+
* This will never be returned and shall always be at the end of the enum. */
81+
CRYPT_ERR_NUM
7882
};
7983

8084
#include "tomcrypt_cfg.h"

src/misc/error_to_string.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Convert error codes to ASCII strings, Tom St Denis
99
*/
1010

11-
static const char * const err_2_str[] =
11+
static const char * const err_2_str[CRYPT_ERR_NUM] =
1212
{
1313
"CRYPT_OK",
1414
"CRYPT_ERROR",
@@ -44,27 +44,26 @@ static const char * const err_2_str[] =
4444

4545
"The input was longer than expected.",
4646

47-
"Invalid sized parameter.",
47+
"Invalid size input for PK parameters.",
4848

4949
"Invalid size for prime.",
50-
5150
"Invalid padding.",
5251

5352
"Hash applied to too many bits.",
54-
5553
"Password context to decrypt key file is missing.",
56-
5754
"The PEM header was not recognized",
5855
};
5956

57+
LTC_STATIC_ASSERT(correct_err_2_str_size, (sizeof(err_2_str)/sizeof(err_2_str[0])) == CRYPT_ERR_NUM)
58+
6059
/**
6160
Convert an LTC error code to ASCII
6261
@param err The error code
6362
@return A pointer to the ASCII NUL terminated string for the error or "Invalid error code." if the err code was not valid.
6463
*/
6564
const char *error_to_string(int err)
6665
{
67-
if (err < 0 || err >= (int)(sizeof(err_2_str)/sizeof(err_2_str[0]))) {
66+
if (err < 0 || err >= CRYPT_ERR_NUM) {
6867
return "Invalid error code.";
6968
}
7069
return err_2_str[err];

0 commit comments

Comments
 (0)