Procházet zdrojové kódy

Introduce `CRYPT_ERR_NUM` and build-time check `err_2_str[]` size

Signed-off-by: Steffen Jaeckel <[email protected]>
Steffen Jaeckel před 10 měsíci
rodič
revize
e487f8400e
2 změnil soubory, kde provedl 9 přidání a 6 odebrání
  1. 4 0
      src/headers/tomcrypt.h
  2. 5 6
      src/misc/error_to_string.c

+ 4 - 0
src/headers/tomcrypt.h

@@ -75,6 +75,10 @@ enum {
    CRYPT_HASH_OVERFLOW,     /* Hash applied to too many bits */
    CRYPT_PW_CTX_MISSING,    /* Password context to decrypt key file is missing */
    CRYPT_UNKNOWN_PEM,       /* The PEM header was not recognized */
+
+   /* Here only follows the number of error codes.
+    * This will never be returned and shall always be at the end of the enum. */
+   CRYPT_ERR_NUM
 };
 
 #include "tomcrypt_cfg.h"

+ 5 - 6
src/misc/error_to_string.c

@@ -8,7 +8,7 @@
   Convert error codes to ASCII strings, Tom St Denis
 */
 
-static const char * const err_2_str[] =
+static const char * const err_2_str[CRYPT_ERR_NUM] =
 {
    "CRYPT_OK",
    "CRYPT_ERROR",
@@ -44,19 +44,18 @@ static const char * const err_2_str[] =
 
    "The input was longer than expected.",
 
-   "Invalid sized parameter.",
+   "Invalid size input for PK parameters.",
 
    "Invalid size for prime.",
-
    "Invalid padding.",
 
    "Hash applied to too many bits.",
-
    "Password context to decrypt key file is missing.",
-
    "The PEM header was not recognized",
 };
 
+LTC_STATIC_ASSERT(correct_err_2_str_size, (sizeof(err_2_str)/sizeof(err_2_str[0])) == CRYPT_ERR_NUM)
+
 /**
    Convert an LTC error code to ASCII
    @param err    The error code
@@ -64,7 +63,7 @@ static const char * const err_2_str[] =
 */
 const char *error_to_string(int err)
 {
-   if (err < 0 || err >= (int)(sizeof(err_2_str)/sizeof(err_2_str[0]))) {
+   if (err < 0 || err >= CRYPT_ERR_NUM) {
       return "Invalid error code.";
    }
    return err_2_str[err];