Browse Source

Merge pull request #415 from libtom/pr/clang-tidy-else-after-return

fix clang-tidy warning: readability-else-after-return
karel-m 7 years ago
parent
commit
4473953742

+ 51 - 0
.ci/clang-tidy.sh

@@ -0,0 +1,51 @@
+#!/bin/bash
+
+# output version
+bash .ci/printinfo.sh
+
+# tested with clang-tidy from llvm-6.0.0
+# not tested with Travis-CI
+
+#### we use the main test sets:
+# readability
+# misc
+# clang-analyzer
+# google
+# performance
+# modernize
+# cert
+# bugprone
+# portability
+
+#### the following checks are skipped
+# google-readability-function-size
+# readability-function-size
+# google-readability-casting
+# readability-braces-around-statements
+# misc-macro-parentheses
+# clang-analyzer-valist.Uninitialized
+
+echo "Run clang-tidy version"
+
+clang-tidy --version || exit 1
+
+echo "Run clang-tidy..."
+
+clang-tidy src/*/*.c src/*/*/*.c src/*/*/*/*.c src/*/*/*/*/*.c -warnings-as-errors='*' --quiet --checks=-*,\
+readability-*,-readability-function-size,-readability-braces-around-statements,\
+misc-*,-misc-macro-parentheses,\
+clang-analyzer-*,-clang-analyzer-valist.Uninitialized,\
+google-*,-google-readability-function-size,-google-readability-casting,\
+performance-*,\
+modernize-*,\
+cert-*,\
+bugprone-*,\
+portability-* -- -DUSE_LTM -DLTM_DESC -Isrc/headers -I../libtommath || { echo "clang-tidy FAILED!"; exit 1; }
+
+echo "clang-tidy ok"
+
+exit 0
+
+# ref:         $Format:%D$
+# git commit:  $Format:%H$
+# commit time: $Format:%ai$

+ 6 - 5
src/ciphers/aes/aes.c

@@ -723,18 +723,19 @@ int ECB_KS(int *keysize)
 {
    LTC_ARGCHK(keysize != NULL);
 
-   if (*keysize < 16)
+   if (*keysize < 16) {
       return CRYPT_INVALID_KEYSIZE;
+   }
    if (*keysize < 24) {
       *keysize = 16;
       return CRYPT_OK;
-   } else if (*keysize < 32) {
+   }
+   if (*keysize < 32) {
       *keysize = 24;
       return CRYPT_OK;
-   } else {
-      *keysize = 32;
-      return CRYPT_OK;
    }
+   *keysize = 32;
+   return CRYPT_OK;
 }
 
 #endif

+ 2 - 1
src/ciphers/blowfish.c

@@ -580,7 +580,8 @@ int blowfish_keysize(int *keysize)
 
    if (*keysize < 8) {
       return CRYPT_INVALID_KEYSIZE;
-   } else if (*keysize > 56) {
+   }
+   if (*keysize > 56) {
       *keysize = 56;
    }
    return CRYPT_OK;

+ 2 - 1
src/ciphers/cast5.c

@@ -707,7 +707,8 @@ int cast5_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 5) {
       return CRYPT_INVALID_KEYSIZE;
-   } else if (*keysize > 16) {
+   }
+   if (*keysize > 16) {
       *keysize = 16;
    }
    return CRYPT_OK;

+ 1 - 2
src/ciphers/kasumi.c

@@ -243,9 +243,8 @@ int kasumi_keysize(int *keysize)
    if (*keysize >= 16) {
       *keysize = 16;
       return CRYPT_OK;
-   } else {
-      return CRYPT_INVALID_KEYSIZE;
    }
+   return CRYPT_INVALID_KEYSIZE;
 }
 
 int kasumi_test(void)

+ 1 - 2
src/ciphers/khazad.c

@@ -843,9 +843,8 @@ int khazad_keysize(int *keysize)
    if (*keysize >= 16) {
       *keysize = 16;
       return CRYPT_OK;
-   } else {
-      return CRYPT_INVALID_KEYSIZE;
    }
+   return CRYPT_INVALID_KEYSIZE;
 }
 
 #endif

+ 2 - 3
src/ciphers/noekeon.c

@@ -314,10 +314,9 @@ int noekeon_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 16) {
       return CRYPT_INVALID_KEYSIZE;
-   } else {
-      *keysize = 16;
-      return CRYPT_OK;
    }
+   *keysize = 16;
+   return CRYPT_OK;
 }
 
 #endif

+ 2 - 1
src/ciphers/rc2.c

@@ -401,7 +401,8 @@ int rc2_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 1) {
        return CRYPT_INVALID_KEYSIZE;
-   } else if (*keysize > 128) {
+   }
+   if (*keysize > 128) {
        *keysize = 128;
    }
    return CRYPT_OK;

+ 2 - 1
src/ciphers/rc5.c

@@ -308,7 +308,8 @@ int rc5_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 8) {
       return CRYPT_INVALID_KEYSIZE;
-   } else if (*keysize > 128) {
+   }
+   if (*keysize > 128) {
       *keysize = 128;
    }
    return CRYPT_OK;

+ 2 - 1
src/ciphers/rc6.c

@@ -318,7 +318,8 @@ int rc6_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 8) {
       return CRYPT_INVALID_KEYSIZE;
-   } else if (*keysize > 128) {
+   }
+   if (*keysize > 128) {
       *keysize = 128;
    }
    return CRYPT_OK;

+ 4 - 6
src/ciphers/safer/safer.c

@@ -358,10 +358,9 @@ int safer_64_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 8) {
       return CRYPT_INVALID_KEYSIZE;
-   } else {
-      *keysize = 8;
-      return CRYPT_OK;
    }
+   *keysize = 8;
+   return CRYPT_OK;
 }
 
 int safer_128_keysize(int *keysize)
@@ -369,10 +368,9 @@ int safer_128_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 16) {
       return CRYPT_INVALID_KEYSIZE;
-   } else {
-      *keysize = 16;
-      return CRYPT_OK;
    }
+   *keysize = 16;
+   return CRYPT_OK;
 }
 
 int safer_k64_test(void)

+ 2 - 1
src/ciphers/skipjack.c

@@ -330,7 +330,8 @@ int skipjack_keysize(int *keysize)
    LTC_ARGCHK(keysize != NULL);
    if (*keysize < 10) {
       return CRYPT_INVALID_KEYSIZE;
-   } else if (*keysize > 10) {
+   }
+   if (*keysize > 10) {
       *keysize = 10;
    }
    return CRYPT_OK;

+ 6 - 7
src/ciphers/twofish/twofish.c

@@ -689,25 +689,24 @@ void twofish_done(symmetric_key *skey)
 int twofish_keysize(int *keysize)
 {
    LTC_ARGCHK(keysize);
-   if (*keysize < 16)
+   if (*keysize < 16) {
       return CRYPT_INVALID_KEYSIZE;
+   }
    if (*keysize < 24) {
       *keysize = 16;
       return CRYPT_OK;
-   } else if (*keysize < 32) {
+   }
+   if (*keysize < 32) {
       *keysize = 24;
       return CRYPT_OK;
-   } else {
-      *keysize = 32;
-      return CRYPT_OK;
    }
+   *keysize = 32;
+   return CRYPT_OK;
 }
 
 #endif
 
 
-
-
 /* ref:         $Format:%D$ */
 /* git commit:  $Format:%H$ */
 /* commit time: $Format:%ai$ */

+ 3 - 3
src/mac/hmac/hmac_test.c

@@ -614,11 +614,11 @@ int hmac_test(void)
 
     if (failed != 0) {
         return CRYPT_FAIL_TESTVECTOR;
-    } else if (tested == 0) {
+    }
+    if (tested == 0) {
         return CRYPT_NOP;
-    } else {
-        return CRYPT_OK;
     }
+    return CRYPT_OK;
  #endif
 }
 

+ 6 - 6
src/misc/base64/base64_decode.c

@@ -104,16 +104,16 @@ static int _base64_decode_internal(const char *in,  unsigned long inlen,
           continue;
        }
        if (c == 253) {
-          if (mode == strict)
+          if (mode == strict) {
              return CRYPT_INVALID_PACKET;
-          else
-             continue; /* allow to ignore white-spaces (relaxed+insane) */
+          }
+          continue; /* allow to ignore white-spaces (relaxed+insane) */
        }
        if (c == 255) {
-          if (mode == insane)
+          if (mode == insane) {
              continue; /* allow to ignore invalid garbage (insane) */
-          else
-             return CRYPT_INVALID_PACKET;
+          }
+          return CRYPT_INVALID_PACKET;
        }
        if ((g > 0) && (mode != insane)) {
           /* we only allow '=' to be at the end (strict+relaxed) */

+ 1 - 2
src/misc/error_to_string.c

@@ -68,9 +68,8 @@ const char *error_to_string(int err)
 {
    if (err < 0 || err >= (int)(sizeof(err_2_str)/sizeof(err_2_str[0]))) {
       return "Invalid error code.";
-   } else {
-      return err_2_str[err];
    }
+   return err_2_str[err];
 }
 
 

+ 1 - 2
src/misc/hkdf/hkdf.c

@@ -30,9 +30,8 @@ int hkdf_extract(int hash_idx, const unsigned char *salt, unsigned long  saltlen
       valid results for HKDF. */
    if (salt == NULL || saltlen == 0) {
       return hmac_memory(hash_idx, (const unsigned char *)"",   1,       in, inlen, out, outlen);
-   } else {
-      return hmac_memory(hash_idx, salt, saltlen, in, inlen, out, outlen);
    }
+   return hmac_memory(hash_idx, salt, saltlen, in, inlen, out, outlen);
 }
 
 int hkdf_expand(int hash_idx, const unsigned char *info, unsigned long infolen,

+ 3 - 3
src/misc/hkdf/hkdf_test.c

@@ -278,11 +278,11 @@ int hkdf_test(void)
 
     if (failed != 0) {
         return CRYPT_FAIL_TESTVECTOR;
-    } else if (tested == 0) {
+    }
+    if (tested == 0) {
         return CRYPT_NOP;
-    } else {
-        return CRYPT_OK;
     }
+    return CRYPT_OK;
  #endif
 }
 

+ 23 - 24
src/modes/cbc/cbc_decrypt.c

@@ -58,32 +58,31 @@ int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
 
    if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
       return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
-   } else {
-      while (len) {
-         /* decrypt */
-         if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
-            return err;
-         }
-
-         /* xor IV against plaintext */
-         #if defined(LTC_FAST)
-         for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
-            tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
-            *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
-            *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
-         }
-    #else
-         for (x = 0; x < cbc->blocklen; x++) {
-            tmpy       = tmp[x] ^ cbc->IV[x];
-            cbc->IV[x] = ct[x];
-            pt[x]      = tmpy;
-         }
-    #endif
+   }
+   while (len) {
+      /* decrypt */
+      if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
+         return err;
+      }
 
-         ct  += cbc->blocklen;
-         pt  += cbc->blocklen;
-         len -= cbc->blocklen;
+      /* xor IV against plaintext */
+#if defined(LTC_FAST)
+      for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
+         tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
+         *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
+         *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
       }
+#else
+      for (x = 0; x < cbc->blocklen; x++) {
+         tmpy       = tmp[x] ^ cbc->IV[x];
+         cbc->IV[x] = ct[x];
+         pt[x]      = tmpy;
+      }
+#endif
+
+      ct  += cbc->blocklen;
+      pt  += cbc->blocklen;
+      len -= cbc->blocklen;
    }
    return CRYPT_OK;
 }

+ 29 - 30
src/modes/cbc/cbc_encrypt.c

@@ -52,39 +52,38 @@ int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s
 
    if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) {
       return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key);
-   } else {
-      while (len) {
-         /* xor IV against plaintext */
-         #if defined(LTC_FAST)
-         for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
-            *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x));
-         }
-    #else
-         for (x = 0; x < cbc->blocklen; x++) {
-            cbc->IV[x] ^= pt[x];
-         }
-    #endif
-
-         /* encrypt */
-         if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) {
-            return err;
-         }
+   }
+   while (len) {
+      /* xor IV against plaintext */
+#if defined(LTC_FAST)
+      for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
+         *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x));
+      }
+#else
+      for (x = 0; x < cbc->blocklen; x++) {
+         cbc->IV[x] ^= pt[x];
+      }
+#endif
 
-         /* store IV [ciphertext] for a future block */
-         #if defined(LTC_FAST)
-         for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
-            *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
-         }
-    #else
-         for (x = 0; x < cbc->blocklen; x++) {
-            cbc->IV[x] = ct[x];
-         }
-    #endif
+      /* encrypt */
+      if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) {
+         return err;
+      }
 
-         ct  += cbc->blocklen;
-         pt  += cbc->blocklen;
-         len -= cbc->blocklen;
+      /* store IV [ciphertext] for a future block */
+#if defined(LTC_FAST)
+      for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
+         *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
+      }
+#else
+      for (x = 0; x < cbc->blocklen; x++) {
+         cbc->IV[x] = ct[x];
       }
+#endif
+
+      ct  += cbc->blocklen;
+      pt  += cbc->blocklen;
+      len -= cbc->blocklen;
    }
    return CRYPT_OK;
 }

+ 7 - 8
src/modes/ecb/ecb_decrypt.c

@@ -39,15 +39,14 @@ int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
    /* check for accel */
    if (cipher_descriptor[ecb->cipher].accel_ecb_decrypt != NULL) {
       return cipher_descriptor[ecb->cipher].accel_ecb_decrypt(ct, pt, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key);
-   } else {
-      while (len) {
-         if ((err = cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key)) != CRYPT_OK) {
-            return err;
-         }
-         pt  += cipher_descriptor[ecb->cipher].block_length;
-         ct  += cipher_descriptor[ecb->cipher].block_length;
-         len -= cipher_descriptor[ecb->cipher].block_length;
+   }
+   while (len) {
+      if ((err = cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key)) != CRYPT_OK) {
+         return err;
       }
+      pt  += cipher_descriptor[ecb->cipher].block_length;
+      ct  += cipher_descriptor[ecb->cipher].block_length;
+      len -= cipher_descriptor[ecb->cipher].block_length;
    }
    return CRYPT_OK;
 }

+ 7 - 8
src/modes/ecb/ecb_encrypt.c

@@ -39,15 +39,14 @@ int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s
    /* check for accel */
    if (cipher_descriptor[ecb->cipher].accel_ecb_encrypt != NULL) {
       return cipher_descriptor[ecb->cipher].accel_ecb_encrypt(pt, ct, len / cipher_descriptor[ecb->cipher].block_length, &ecb->key);
-   } else {
-      while (len) {
-         if ((err = cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key)) != CRYPT_OK) {
-            return err;
-         }
-         pt  += cipher_descriptor[ecb->cipher].block_length;
-         ct  += cipher_descriptor[ecb->cipher].block_length;
-         len -= cipher_descriptor[ecb->cipher].block_length;
+   }
+   while (len) {
+      if ((err = cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key)) != CRYPT_OK) {
+         return err;
       }
+      pt  += cipher_descriptor[ecb->cipher].block_length;
+      ct  += cipher_descriptor[ecb->cipher].block_length;
+      len -= cipher_descriptor[ecb->cipher].block_length;
    }
    return CRYPT_OK;
 }

+ 4 - 2
src/pk/asn1/der/general/der_decode_asn1_length.c

@@ -41,9 +41,11 @@ int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsign
       real_len &= 0x7F;
       if (real_len == 0) {
          return CRYPT_PK_ASN1_ERROR;
-      } else if (real_len > sizeof(decoded_len)) {
+      }
+      if (real_len > sizeof(decoded_len)) {
          return CRYPT_OVERFLOW;
-      } else if (real_len > (*inlen - 1)) {
+      }
+      if (real_len > (*inlen - 1)) {
          return CRYPT_BUFFER_OVERFLOW;
       }
       decoded_len = 0;

+ 9 - 10
src/pk/asn1/der/general/der_encode_asn1_identifier.c

@@ -41,16 +41,15 @@ int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsi
       }
       *outlen = 1;
       return CRYPT_OK;
-   } else {
-      if (id->klass < LTC_ASN1_CL_UNIVERSAL || id->klass > LTC_ASN1_CL_PRIVATE) {
-         return CRYPT_INVALID_ARG;
-      }
-      if (id->pc < LTC_ASN1_PC_PRIMITIVE || id->pc > LTC_ASN1_PC_CONSTRUCTED) {
-         return CRYPT_INVALID_ARG;
-      }
-      if (id->tag > (ULONG_MAX >> (8 + 7))) {
-         return CRYPT_INVALID_ARG;
-      }
+   }
+   if (id->klass < LTC_ASN1_CL_UNIVERSAL || id->klass > LTC_ASN1_CL_PRIVATE) {
+      return CRYPT_INVALID_ARG;
+   }
+   if (id->pc < LTC_ASN1_PC_PRIMITIVE || id->pc > LTC_ASN1_PC_CONSTRUCTED) {
+      return CRYPT_INVALID_ARG;
+   }
+   if (id->tag > (ULONG_MAX >> (8 + 7))) {
+      return CRYPT_INVALID_ARG;
    }
 
    if (out != NULL) {

+ 5 - 4
src/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c

@@ -112,7 +112,8 @@ YYYYMMDDhhmmss.fs-hh'mm'
     /* now is it Z or . */
     if (buf[x] == 'Z') {
        return CRYPT_OK;
-    } else if (buf[x] == '.') {
+    }
+    if (buf[x] == '.') {
        x++;
        while (buf[x] >= '0' && buf[x] <= '9') {
           unsigned fs = out->fs;
@@ -127,14 +128,14 @@ YYYYMMDDhhmmss.fs-hh'mm'
     /* now is it Z, +, - */
     if (buf[x] == 'Z') {
        return CRYPT_OK;
-    } else if (buf[x] == '+' || buf[x] == '-') {
+    }
+    if (buf[x] == '+' || buf[x] == '-') {
        out->off_dir = (buf[x++] == '+') ? 0 : 1;
        DECODE_V(out->off_hh, 24);
        DECODE_V(out->off_mm, 60);
        return CRYPT_OK;
-    } else {
-       return CRYPT_INVALID_PACKET;
     }
+    return CRYPT_INVALID_PACKET;
 }
 
 #endif

+ 1 - 2
src/pk/asn1/der/set/der_encode_set.c

@@ -33,9 +33,8 @@ static int _qsort_helper(const void *a, const void *b)
    if (r == 0) {
       /* their order in the original list now determines the position */
       return A->used - B->used;
-   } else {
-      return r;
    }
+   return r;
 }
 
 /*

+ 5 - 4
src/pk/asn1/der/utctime/der_decode_utctime.c

@@ -95,7 +95,8 @@ YYMMDDhhmmss-hh'mm'
     /* now is it Z, +, - or 0-9 */
     if (buf[x] == 'Z') {
        return CRYPT_OK;
-    } else if (buf[x] == '+' || buf[x] == '-') {
+    }
+    if (buf[x] == '+' || buf[x] == '-') {
        out->off_dir = (buf[x++] == '+') ? 0 : 1;
        DECODE_V(out->off_hh, 24);
        DECODE_V(out->off_mm, 60);
@@ -108,14 +109,14 @@ YYMMDDhhmmss-hh'mm'
     /* now is it Z, +, - */
     if (buf[x] == 'Z') {
        return CRYPT_OK;
-    } else if (buf[x] == '+' || buf[x] == '-') {
+    }
+    if (buf[x] == '+' || buf[x] == '-') {
        out->off_dir = (buf[x++] == '+') ? 0 : 1;
        DECODE_V(out->off_hh, 24);
        DECODE_V(out->off_mm, 60);
        return CRYPT_OK;
-    } else {
-       return CRYPT_INVALID_PACKET;
     }
+    return CRYPT_INVALID_PACKET;
 }
 
 #endif

+ 6 - 7
src/pk/asn1/der/utf8/der_length_utf8_string.c

@@ -23,18 +23,17 @@ unsigned long der_utf8_charsize(const wchar_t c)
 {
    if (c <= 0x7F) {
       return 1;
-   } else if (c <= 0x7FF) {
+   }
+   if (c <= 0x7FF) {
       return 2;
-#if LTC_WCHAR_MAX == 0xFFFF
-   } else {
-      return 3;
    }
+#if LTC_WCHAR_MAX == 0xFFFF
+   return 3;
 #else
-   } else if (c <= 0xFFFF) {
+   if (c <= 0xFFFF) {
       return 3;
-   } else {
-      return 4;
    }
+   return 4;
 #endif
 }
 

+ 7 - 9
src/pk/dh/dh_generate_key.c

@@ -19,27 +19,25 @@ static int _dh_groupsize_to_keysize(int groupsize)
    if (groupsize <= 0) {
       return 0;
    }
-   else if (groupsize <= 192) {
+   if (groupsize <= 192) {
       return 30;     /* 1536-bit => key size 240-bit */
    }
-   else if (groupsize <= 256) {
+   if (groupsize <= 256) {
       return 40;     /* 2048-bit => key size 320-bit */
    }
-   else if (groupsize <= 384) {
+   if (groupsize <= 384) {
       return 52;     /* 3072-bit => key size 416-bit */
    }
-   else if (groupsize <= 512) {
+   if (groupsize <= 512) {
       return 60;     /* 4096-bit => key size 480-bit */
    }
-   else if (groupsize <= 768) {
+   if (groupsize <= 768) {
       return 67;     /* 6144-bit => key size 536-bit */
    }
-   else if (groupsize <= 1024) {
+   if (groupsize <= 1024) {
       return 77;     /* 8192-bit => key size 616-bit */
    }
-   else {
-      return 0;
-   }
+   return 0;
 }
 
 int dh_generate_key(prng_state *prng, int wprng, dh_key *key)

+ 10 - 16
src/pk/dsa/dsa_export.c

@@ -26,6 +26,7 @@
 int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_key *key)
 {
    unsigned long zero=0;
+   unsigned char flags[1];
    int err, std;
 
    LTC_ARGCHK(out    != NULL);
@@ -35,15 +36,10 @@ int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_ke
    std = type & PK_STD;
    type &= ~PK_STD;
 
-   /* can we store the static header?  */
    if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
       return CRYPT_PK_TYPE_MISMATCH;
    }
 
-   if (type != PK_PUBLIC && type != PK_PRIVATE) {
-      return CRYPT_INVALID_ARG;
-   }
-
    if (type == PK_PRIVATE) {
       if (std) {
           return der_encode_sequence_multi(out, outlen,
@@ -55,10 +51,8 @@ int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_ke
                                          LTC_ASN1_INTEGER,      1UL, key->x,
                                          LTC_ASN1_EOL,          0UL, NULL);
       }
-      else {
-          unsigned char flags[1];
-          flags[0] = 1;
-          return der_encode_sequence_multi(out, outlen,
+      flags[0] = 1;
+      return der_encode_sequence_multi(out, outlen,
                                          LTC_ASN1_BIT_STRING,   1UL, flags,
                                          LTC_ASN1_INTEGER,      1UL, key->g,
                                          LTC_ASN1_INTEGER,      1UL, key->p,
@@ -66,8 +60,9 @@ int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_ke
                                          LTC_ASN1_INTEGER,      1UL, key->y,
                                          LTC_ASN1_INTEGER,      1UL, key->x,
                                          LTC_ASN1_EOL,          0UL, NULL);
-      }
-   } else {
+   }
+
+   if (type == PK_PUBLIC) {
       if (std) {
           unsigned long tmplen = (unsigned long)(mp_count_bits(key->y) / 8) + 8;
           unsigned char* tmp = XMALLOC(tmplen);
@@ -94,18 +89,17 @@ error:
           XFREE(tmp);
           return err;
       }
-      else {
-          unsigned char flags[1];
-          flags[0] = 0;
-          return der_encode_sequence_multi(out, outlen,
+      flags[0] = 0;
+      return der_encode_sequence_multi(out, outlen,
                                      LTC_ASN1_BIT_STRING,   1UL, flags,
                                      LTC_ASN1_INTEGER,      1UL, key->g,
                                      LTC_ASN1_INTEGER,      1UL, key->p,
                                      LTC_ASN1_INTEGER,      1UL, key->q,
                                      LTC_ASN1_INTEGER,      1UL, key->y,
                                      LTC_ASN1_EOL,          0UL, NULL);
-      }
    }
+
+   return CRYPT_INVALID_ARG;
 }
 
 #endif

+ 2 - 4
src/pk/ecc/ecc_get_curve.c

@@ -208,10 +208,8 @@ static int _name_match(const char *left, const char *right)
       right++;
    }
 
-   if ((*left == '\0') && (*right == '\0'))
-      return 1;
-   else
-      return 0;
+   if ((*left == '\0') && (*right == '\0')) return 1;
+   return 0;
 }
 
 int ecc_get_curve(const char *name_or_oid, const ltc_ecc_curve **cu)

+ 13 - 8
src/pk/rsa/rsa_export.c

@@ -26,14 +26,16 @@
 int rsa_export(unsigned char *out, unsigned long *outlen, int type, const rsa_key *key)
 {
    unsigned long zero=0;
-   int err;
+   int err, std;
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(outlen != NULL);
    LTC_ARGCHK(key    != NULL);
 
-   /* type valid? */
-   if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) {
-      return CRYPT_PK_INVALID_TYPE;
+   std = type & PK_STD;
+   type &= ~PK_STD;
+
+   if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
+      return CRYPT_PK_TYPE_MISMATCH;
    }
 
    if (type == PK_PRIVATE) {
@@ -52,12 +54,14 @@ int rsa_export(unsigned char *out, unsigned long *outlen, int type, const rsa_ke
                           LTC_ASN1_INTEGER, 1UL,  key->dQ,
                           LTC_ASN1_INTEGER, 1UL,  key->qP,
                           LTC_ASN1_EOL,     0UL, NULL);
-   } else {
+   }
+
+   if (type == PK_PUBLIC) {
       /* public key */
       unsigned long tmplen, *ptmplen;
       unsigned char* tmp = NULL;
 
-      if (type & PK_STD) {
+      if (std) {
           tmplen = (unsigned long)(mp_count_bits(key->N) / 8) * 2 + 8;
           tmp = XMALLOC(tmplen);
           ptmplen = &tmplen;
@@ -75,7 +79,7 @@ int rsa_export(unsigned char *out, unsigned long *outlen, int type, const rsa_ke
                                  LTC_ASN1_INTEGER, 1UL,  key->e,
                                  LTC_ASN1_EOL,     0UL, NULL);
 
-      if ((err != CRYPT_OK) || !(type & PK_STD)) {
+      if ((err != CRYPT_OK) || !std) {
           goto finish;
       }
 
@@ -86,8 +90,9 @@ finish:
       if (tmp != out)
         XFREE(tmp);
       return err;
-
    }
+
+   return CRYPT_INVALID_ARG;
 }
 
 #endif /* LTC_MRSA */

+ 5 - 6
src/stream/rabbit/rabbit.c

@@ -297,13 +297,12 @@ int rabbit_crypt(rabbit_state* st, const unsigned char *in, unsigned long inlen,
        /* copy remainder to block */
        for (i = inlen; i < 16; ++i) st->block[i] = buf[i];
        return CRYPT_OK;
-     } else {
-       /* XOR entire buf and send to out */
-       for (i = 0; i < 16; ++i) out[i] = in[i] ^ buf[i];
-       inlen -= 16;
-       out += 16;
-       in  += 16;
      }
+     /* XOR entire buf and send to out */
+     for (i = 0; i < 16; ++i) out[i] = in[i] ^ buf[i];
+     inlen -= 16;
+     out += 16;
+     in  += 16;
    }
 }