On 21.04.14 01:13, Bob Beck wrote:

> this list is for diffs.  post them.  keep them reviewable - of
> meaningful size and doing a certain thing.
> (as opposed to this diff changes 15 things.. )

Find attached my patches for some memory leaks, use after frees and some
minor house keeping as follows.

======== 8< ======== 8< ======== 8< ======== 8< ======== 8< ======== 8<

memleak_realloc.patch
* fix memory leak in a2i_ASN1_ENUMERATED, a2i_ASN1_STRING
  and a2i_ASN1_INTEGER, in case of goto err
* use realloc instead of s ? realloc(s,l) : malloc(l)
* Files: f_enum.c f_int.c f_string.c

double_free.patch
* remove unnecessary NULL checks before free()
* fix double free in d2i_ASN1_bytes by setting ret->data = NULL
  after free, before potential goto err;
* Files: a_bytes.c

calloc_length.patch
* remove unnecessary NULL checks before free()
* use calloc instead of malloc and memset
* remove unnecessary temp variable d
* move loop counter j in for() header
* make calculation of actual length in BN_to_ASN1_ENUMERATED
  more transparent
* Files: a_enum.c a_int.c

bad_macros.patch
* remove M_ASN1_New_Malloc, M_ASN1_New, M_ASN1_New_Error marcos,
  they hide a malloc and are only used once
* Files: asn1_mac.h x_pkey.c

nullcheck.patch
* remove unnecessary NULL checks before free()
* Files: a_bitstr.c a_gentm.c a_object.c a_utctm.c ameth_lib.c
  asn1_gen.c asn_mime.c asn_pack.c bio_asn1.c bio_ndef.c t_x509.c
  tasn_dec.c tasn_utl.c x_info.c x_name.c

calloc_realloc.patch
* remove unnecessary NULL checks before free()
* use calloc instead of malloc and memset
* use realloc instead of s ? realloc(s,l) : malloc(l)
* Files: ameth_lib.c asn1_lib.c tasn_new.c

sizeof.patch
* use sizeof(buf) instead of numeric value for passing
  to OPENSSL_cleanse
* Files: n_pkey.c

double_null.patch
* remove unnecessary double *pval = NULL in ASN1_primitive_free
* Files: tasn_fre.c

reuse.patch
* remove unnecessary NULL checks before free()
* prevent potential use after free of ret->name in x509_cb
* Files: x_x509.c

======== 8< ======== 8< ======== 8< ======== 8< ======== 8< ======== 8<

Some notes on my first glimpse at the code:

* ASN1_PCTX_new could use a calloc and avoid filling all members
* ASN1_BIT_STRING_set_bit and a2i_ASN1_INTEGER use CRYPTO_realloc_clean
  which OPENSSL_cleanse old location, in similar realloc settings,
  normal realloc was (mis)used
* still OPENSSL_free inside
* different styles for checking !p vs p == NULL vs if(!(p=malloc()))
* ASN1_STRING_set looks like a bloated and overly complex strdup wrapper
* ASN1_INTEGER_set and ASN1_ENUMERATED_set use some rather peculiar
  buffer on the stack which is then copied backwards to allocation

> #define LENGTHIKNOWNOTHINGABOUT 288 + 1
> 
> If you're doing that, you're just hiding the horror and making it
> harder for someone like
> me to fix it.

I rather thought about copying constants from the RFC and naming offsets
and lengths into structures as such, also using sizeof(buf) instead of
the hard coded value as in sizeof.patch.

Regards,

  erdgeist
Index: asn1_mac.h
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/asn1_mac.h,v
retrieving revision 1.10
diff -u -r1.10 asn1_mac.h
--- asn1_mac.h  17 Apr 2014 13:37:48 -0000      1.10
+++ asn1_mac.h  21 Apr 2014 02:19:24 -0000
@@ -287,21 +287,6 @@
                c.slen-=(c.p-c.q); \
                }
 
-/* New macros */
-#define M_ASN1_New_Malloc(ret,type) \
-       if ((ret=(type *)malloc(sizeof(type))) == NULL) \
-               { c.line=__LINE__; goto err2; }
-
-#define M_ASN1_New(arg,func) \
-       if (((arg)=func()) == NULL) return(NULL)
-
-#define M_ASN1_New_Error(a) \
-/*     err:    ASN1_MAC_H_err((a),ERR_R_NESTED_ASN1_ERROR,c.line); \
-               return(NULL);*/ \
-       err2:   ASN1_MAC_H_err((a),ERR_R_MALLOC_FAILURE,c.line); \
-               return(NULL)
-
-
 /* BIG UGLY WARNING!  This is so damn ugly I wanna puke.  Unfortunately,
    some macros that use ASN1_const_CTX still insist on writing in the input
    stream.  ARGH!  ARGH!  ARGH!  Let's get rid of this macro package.
Index: x_pkey.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/x_pkey.c,v
retrieving revision 1.10
diff -u -r1.10 x_pkey.c
--- x_pkey.c    18 Apr 2014 11:20:32 -0000      1.10
+++ x_pkey.c    21 Apr 2014 02:19:24 -0000
@@ -109,10 +109,17 @@
        X509_PKEY *ret = NULL;
        ASN1_CTX c;
 
-       M_ASN1_New_Malloc(ret, X509_PKEY);
+       if ((ret = malloc(sizeof(X509_PKEY))) == NULL )
+               ASN1_MAC_H_err(ASN1_F_X509_PKEY_NEW, ERR_R_MALLOC_FAILURE, 
__LINE__ );
+               return NULL;
+       }
        ret->version = 0;
-       M_ASN1_New(ret->enc_algor, X509_ALGOR_new);
-       M_ASN1_New(ret->enc_pkey, M_ASN1_OCTET_STRING_new);
+       ret->enc_algor = X509_ALGOR_new();
+       if (ret->enc_algor == NULL)
+               return NULL;
+       ret->enc_pkey = M_ASN1_OCTET_STRING_new();
+       if (ret->enc_pkey == NULL)
+               return NULL;
        ret->dec_pkey = NULL;
        ret->key_length = 0;
        ret->key_data = NULL;
@@ -121,7 +128,6 @@
        memset(ret->cipher.iv, 0, EVP_MAX_IV_LENGTH);
        ret->references = 1;
        return (ret);
-       M_ASN1_New_Error(ASN1_F_X509_PKEY_NEW);
 }
 
 void
@@ -142,7 +148,7 @@
                M_ASN1_OCTET_STRING_free(x->enc_pkey);
        if (x->dec_pkey != NULL)
                EVP_PKEY_free(x->dec_pkey);
-       if ((x->key_data != NULL) && (x->key_free))
+       if (x->key_free)
                free(x->key_data);
        free(x);
 }
Index: a_enum.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/a_enum.c,v
retrieving revision 1.11
diff -u -r1.11 a_enum.c
--- a_enum.c    18 Apr 2014 07:09:23 -0000      1.11
+++ a_enum.c    21 Apr 2014 02:19:07 -0000
@@ -72,33 +72,28 @@
        int j, k;
        unsigned int i;
        unsigned char buf[sizeof(long) + 1];
-       long d;
 
        a->type = V_ASN1_ENUMERATED;
        if (a->length < (int)(sizeof(long) + 1)) {
-               if (a->data != NULL)
-                       free(a->data);
-               if ((a->data = (unsigned char *)malloc(sizeof(long) + 1)) != 
NULL)
-                       memset((char *)a->data, 0, sizeof(long) + 1);
+               free(a->data);
+               a->data = (unsigned char *)calloc(1, sizeof(long) + 1);
        }
        if (a->data == NULL) {
                ASN1err(ASN1_F_ASN1_ENUMERATED_SET, ERR_R_MALLOC_FAILURE);
                return (0);
        }
-       d = v;
-       if (d < 0) {
-               d = -d;
+       if (v < 0) {
+               v = -v;
                a->type = V_ASN1_NEG_ENUMERATED;
        }
 
        for (i = 0; i < sizeof(long); i++) {
-               if (d == 0)
+               if (v == 0)
                        break;
-               buf[i] = (int)d & 0xff;
-               d >>= 8;
+               buf[i] = (unsigned char)v;
+               v >>= 8;
        }
-       j = 0;
-       for (k = i - 1; k >=0; k--)
+       for (j = 0, k = i - 1; k >=0; k--)
                a->data[j++] = buf[k];
        a->length = j;
        return (1);
@@ -138,7 +133,7 @@
 BN_to_ASN1_ENUMERATED(BIGNUM *bn, ASN1_ENUMERATED *ai)
 {
        ASN1_ENUMERATED *ret;
-       int len, j;
+       int len;
 
        if (ai == NULL)
                ret = M_ASN1_ENUMERATED_new();
@@ -152,10 +147,10 @@
                ret->type = V_ASN1_NEG_ENUMERATED;
        else
                ret->type = V_ASN1_ENUMERATED;
-       j = BN_num_bits(bn);
-       len = ((j == 0) ? 0 : ((j / 8) + 1));
-       if (ret->length < len + 4) {
-               unsigned char *new_data = realloc(ret->data, len + 4);
+       len = BN_num_bits(bn);
+       len = 4 + ( len ? len / 8 + 1 : 0 );
+       if (ret->length < len) {
+               unsigned char *new_data = realloc(ret->data, len);
                if (!new_data) {
                        ASN1err(ASN1_F_BN_TO_ASN1_ENUMERATED, 
ERR_R_MALLOC_FAILURE);
                        goto err;
Index: a_int.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/a_int.c,v
retrieving revision 1.17
diff -u -r1.17 a_int.c
--- a_int.c     19 Apr 2014 12:51:01 -0000      1.17
+++ a_int.c     21 Apr 2014 02:19:08 -0000
@@ -159,14 +159,14 @@
        if (a->length == 0)
                *(p++) = 0;
        else if (!neg)
-               memcpy(p, a->data, (unsigned int)a->length);
+               memcpy(p, a->data, (size_t)a->length);
        else {
                /* Begin at the end of the encoding */
                n = a->data + a->length - 1;
                p += a->length - 1;
                i = a->length;
                /* Copy zeros to destination as long as source is zero */
-               while (!*n) {
+               while (*n == 0) {
                        *(p--) = 0;
                        n--;
                        i--;
@@ -256,8 +256,7 @@
                memcpy(s, p, (int)len);
        }
 
-       if (ret->data != NULL)
-               free(ret->data);
+       free(ret->data);
        ret->data = s;
        ret->length = (int)len;
        if (a != NULL)
@@ -324,8 +323,7 @@
                p += len;
        }
 
-       if (ret->data != NULL)
-               free(ret->data);
+       free(ret->data);
        ret->data = s;
        ret->length = (int)len;
        if (a != NULL)
@@ -346,33 +344,28 @@
        int j, k;
        unsigned int i;
        unsigned char buf[sizeof(long) + 1];
-       long d;
 
        a->type = V_ASN1_INTEGER;
        if (a->length < (int)(sizeof(long) + 1)) {
-               if (a->data != NULL)
-                       free(a->data);
-               if ((a->data = (unsigned char *)malloc(sizeof(long) + 1)) != 
NULL)
-                       memset((char *)a->data, 0, sizeof(long) + 1);
+               free(a->data);
+               a->data = (unsigned char *)calloc(1, sizeof(long) + 1);
        }
        if (a->data == NULL) {
                ASN1err(ASN1_F_ASN1_INTEGER_SET, ERR_R_MALLOC_FAILURE);
                return (0);
        }
-       d = v;
-       if (d < 0) {
-               d = -d;
+       if (v < 0) {
+               v = -v;
                a->type = V_ASN1_NEG_INTEGER;
        }
 
        for (i = 0; i < sizeof(long); i++) {
-               if (d == 0)
+               if (v == 0)
                        break;
-               buf[i] = (int)d & 0xff;
-               d >>= 8;
+               buf[i] = (int)v & 0xff;
+               v >>= 8;
        }
-       j = 0;
-       for (k = i - 1; k >= 0; k--)
+       for (j = 0, k = i - 1; k >= 0; k--)
                a->data[j++] = buf[k];
        a->length = j;
        return (1);
@@ -412,7 +405,7 @@
 BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
 {
        ASN1_INTEGER *ret;
-       int len, j;
+       int len;
 
        if (ai == NULL)
                ret = M_ASN1_INTEGER_new();
@@ -426,10 +419,10 @@
                ret->type = V_ASN1_NEG_INTEGER;
        else
                ret->type = V_ASN1_INTEGER;
-       j = BN_num_bits(bn);
-       len = ((j == 0) ? 0 : ((j / 8) + 1));
-       if (ret->length < len + 4) {
-               unsigned char *new_data = realloc(ret->data, len + 4);
+       len = BN_num_bits(bn);
+       len = 4 + ( len ? len / 8 + 1 : 0 );
+       if (ret->length < len) {
+               unsigned char *new_data = realloc(ret->data, len);
                if (!new_data) {
                        ASN1err(ASN1_F_BN_TO_ASN1_INTEGER, 
ERR_R_MALLOC_FAILURE);
                        goto err;
Index: ameth_lib.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/ameth_lib.c,v
retrieving revision 1.5
diff -u -r1.5 ameth_lib.c
--- ameth_lib.c 18 Apr 2014 13:26:34 -0000      1.5
+++ ameth_lib.c 21 Apr 2014 02:18:08 -0000
@@ -287,12 +287,10 @@
 {
        EVP_PKEY_ASN1_METHOD *ameth;
 
-       ameth = malloc(sizeof(EVP_PKEY_ASN1_METHOD));
+       ameth = calloc(1, sizeof(EVP_PKEY_ASN1_METHOD));
        if (!ameth)
                return NULL;
 
-       memset(ameth, 0, sizeof(EVP_PKEY_ASN1_METHOD));
-
        ameth->pkey_id = id;
        ameth->pkey_base_id = id;
        ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
@@ -382,10 +380,8 @@
 EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
 {
        if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
-               if (ameth->pem_str)
-                       free(ameth->pem_str);
-               if (ameth->info)
-                       free(ameth->info);
+               free(ameth->pem_str);
+               free(ameth->info);
                free(ameth);
        }
 }
Index: asn1_lib.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/asn1_lib.c,v
retrieving revision 1.22
diff -u -r1.22 asn1_lib.c
--- asn1_lib.c  19 Apr 2014 11:43:07 -0000      1.22
+++ asn1_lib.c  21 Apr 2014 02:18:08 -0000
@@ -373,7 +373,6 @@
 int
 ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
 {
-       unsigned char *c;
        const char *data = _data;
 
        if (len < 0) {
@@ -383,17 +382,13 @@
                        len = strlen(data);
        }
        if ((str->length < len) || (str->data == NULL)) {
-               c = str->data;
-               if (c == NULL)
-                       str->data = malloc(len + 1);
-               else
-                       str->data = realloc(c, len + 1);
+               unsigned char *c = realloc(str->data, len + 1);
 
-               if (str->data == NULL) {
+               if (c == NULL) {
                        ASN1err(ASN1_F_ASN1_STRING_SET, ERR_R_MALLOC_FAILURE);
-                       str->data = c;
                        return (0);
                }
+               str->data = c;
        }
        str->length = len;
        if (data != NULL) {
@@ -407,8 +402,7 @@
 void
 ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
 {
-       if (str->data)
-               free(str->data);
+       free(str->data);
        str->data = data;
        str->length = len;
 }
@@ -441,7 +435,7 @@
 {
        if (a == NULL)
                return;
-       if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
+       if (!a->flags & ASN1_STRING_FLAG_NDEF)
                free(a->data);
        free(a);
 }
Index: tasn_new.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/tasn_new.c,v
retrieving revision 1.9
diff -u -r1.9 tasn_new.c
--- tasn_new.c  18 Apr 2014 12:15:48 -0000      1.9
+++ tasn_new.c  21 Apr 2014 02:18:08 -0000
@@ -156,10 +156,9 @@
                        }
                }
                if (!combine) {
-                       *pval = malloc(it->size);
+                       *pval = calloc(1, it->size);
                        if (!*pval)
                                goto memerr;
-                       memset(*pval, 0, it->size);
                }
                asn1_set_choice_selector(pval, -1, it);
                if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
@@ -181,10 +180,9 @@
                        }
                }
                if (!combine) {
-                       *pval = malloc(it->size);
+                       *pval = calloc(1, it->size);
                        if (!*pval)
                                goto memerr;
-                       memset(*pval, 0, it->size);
                        asn1_do_lock(pval, 0, it);
                        asn1_enc_init(pval, it);
                }
Index: a_bytes.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/a_bytes.c,v
retrieving revision 1.10
diff -u -r1.10 a_bytes.c
--- a_bytes.c   17 Apr 2014 16:14:15 -0000      1.10
+++ a_bytes.c   21 Apr 2014 02:18:34 -0000
@@ -110,8 +110,7 @@
        } else
                s = NULL;
 
-       if (ret->data != NULL)
-               free(ret->data);
+       free(ret->data);
        ret->length = (int)len;
        ret->data = s;
        ret->type = tag;
@@ -203,8 +202,8 @@
        } else {
                if (len != 0) {
                        if ((ret->length < len) || (ret->data == NULL)) {
-                               if (ret->data != NULL)
-                                       free(ret->data);
+                               free(ret->data);
+                               ret->data = NULL;
                                s = (unsigned char *)malloc((int)len + 1);
                                if (s == NULL) {
                                        i = ERR_R_MALLOC_FAILURE;
@@ -217,8 +216,7 @@
                        p += len;
                } else {
                        s = NULL;
-                       if (ret->data != NULL)
-                               free(ret->data);
+                       free(ret->data);
                }
 
                ret->length = (int)len;
@@ -292,8 +290,7 @@
                goto err;
 
        a->length = num;
-       if (a->data != NULL)
-               free(a->data);
+       free(a->data);
        a->data = (unsigned char *)b.data;
        if (os != NULL)
                ASN1_STRING_free(os);
@@ -303,7 +300,6 @@
        ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE, c->error);
        if (os != NULL)
                ASN1_STRING_free(os);
-       if (b.data != NULL)
-               free(b.data);
+       free(b.data);
        return (0);
 }
Index: tasn_fre.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/tasn_fre.c,v
retrieving revision 1.9
diff -u -r1.9 tasn_fre.c
--- tasn_fre.c  18 Apr 2014 12:15:48 -0000      1.9
+++ tasn_fre.c  21 Apr 2014 02:19:57 -0000
@@ -247,7 +247,6 @@
 
        default:
                ASN1_STRING_free((ASN1_STRING *)*pval);
-               *pval = NULL;
                break;
        }
        *pval = NULL;
Index: f_enum.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/f_enum.c,v
retrieving revision 1.7
diff -u -r1.7 f_enum.c
--- f_enum.c    19 Apr 2014 06:43:34 -0000      1.7
+++ f_enum.c    21 Apr 2014 02:17:33 -0000
@@ -154,17 +154,9 @@
                }
                i /= 2;
                if (num + i > slen) {
-                       if (s == NULL)
-                               sp = (unsigned char *)malloc(
-                                   (unsigned int)num + i * 2);
-                       else
-                               sp = (unsigned char *)realloc(s,
-                                   (unsigned int)num + i * 2);
+                       sp = (unsigned char *)realloc(s, (unsigned int)num + i 
* 2);
                        if (sp == NULL) {
-                               ASN1err(ASN1_F_A2I_ASN1_ENUMERATED,
-                                   ERR_R_MALLOC_FAILURE);
-                               if (s != NULL)
-                                       free(s);
+                               ASN1err(ASN1_F_A2I_ASN1_ENUMERATED, 
ERR_R_MALLOC_FAILURE);
                                goto err;
                        }
                        s = sp;
@@ -203,5 +195,6 @@
 err_sl:
                ASN1err(ASN1_F_A2I_ASN1_ENUMERATED, ASN1_R_SHORT_LINE);
        }
+       free(s);
        return (ret);
 }
Index: f_int.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/f_int.c,v
retrieving revision 1.11
diff -u -r1.11 f_int.c
--- f_int.c     19 Apr 2014 06:43:34 -0000      1.11
+++ f_int.c     21 Apr 2014 02:17:33 -0000
@@ -167,8 +167,6 @@
                        if (sp == NULL) {
                                ASN1err(ASN1_F_A2I_ASN1_INTEGER,
                                    ERR_R_MALLOC_FAILURE);
-                               if (s != NULL)
-                                       free(s);
                                goto err;
                        }
                        s = sp;
@@ -207,5 +205,6 @@
 err_sl:
                ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_SHORT_LINE);
        }
+       free(s)
        return (ret);
 }
Index: f_string.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/f_string.c,v
retrieving revision 1.9
diff -u -r1.9 f_string.c
--- f_string.c  19 Apr 2014 06:43:34 -0000      1.9
+++ f_string.c  21 Apr 2014 02:17:33 -0000
@@ -150,17 +150,9 @@
                }
                i /= 2;
                if (num + i > slen) {
-                       if (s == NULL)
-                               sp = (unsigned char *)malloc(
-                                   (unsigned int)num + i * 2);
-                       else
-                               sp = (unsigned char *)realloc(s,
-                                   (unsigned int)num + i * 2);
+                       sp = (unsigned char *)realloc(s, (unsigned int)num + i 
* 2);
                        if (sp == NULL) {
-                               ASN1err(ASN1_F_A2I_ASN1_STRING,
-                                   ERR_R_MALLOC_FAILURE);
-                               if (s != NULL)
-                                       free(s);
+                               ASN1err(ASN1_F_A2I_ASN1_STRING, 
ERR_R_MALLOC_FAILURE);
                                goto err;
                        }
                        s = sp;
@@ -199,5 +191,6 @@
 err_sl:
                ASN1err(ASN1_F_A2I_ASN1_STRING, ASN1_R_SHORT_LINE);
        }
+       free(s);
        return (ret);
 }
Index: a_bitstr.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/a_bitstr.c,v
retrieving revision 1.14
diff -u -r1.14 a_bitstr.c
--- a_bitstr.c  18 Apr 2014 07:09:23 -0000      1.14
+++ a_bitstr.c  21 Apr 2014 02:17:02 -0000
@@ -165,8 +165,7 @@
                s = NULL;
 
        ret->length = (int)len;
-       if (ret->data != NULL)
-               free(ret->data);
+       free(ret->data);
        ret->data = s;
        ret->type = V_ASN1_BIT_STRING;
        if (a != NULL)
Index: a_gentm.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/a_gentm.c,v
retrieving revision 1.17
diff -u -r1.17 a_gentm.c
--- a_gentm.c   19 Apr 2014 11:43:07 -0000      1.17
+++ a_gentm.c   21 Apr 2014 02:17:02 -0000
@@ -239,8 +239,7 @@
                            ERR_R_MALLOC_FAILURE);
                        return (NULL);
                }
-               if (s->data != NULL)
-                       free(s->data);
+               free(s->data);
                s->data = (unsigned char *)p;
        }
 
Index: a_object.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/a_object.c,v
retrieving revision 1.14
diff -u -r1.14 a_object.c
--- a_object.c  19 Apr 2014 12:51:01 -0000      1.14
+++ a_object.c  21 Apr 2014 02:17:02 -0000
@@ -310,8 +310,7 @@
        /* once detached we can change it */
        if ((data == NULL) || (ret->length < len)) {
                ret->length = 0;
-               if (data != NULL)
-                       free(data);
+               free(data);
                data = (unsigned char *)malloc(len ? (int)len : 1);
                if (data == NULL) {
                        i = ERR_R_MALLOC_FAILURE;
@@ -366,16 +365,13 @@
                return;
        if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
 #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. 
Doing this on a "real" compile will cause memory leaks */
-               if (a->sn != NULL)
-                       free((void *)a->sn);
-               if (a->ln != NULL)
-                       free((void *)a->ln);
+               free((void *)a->sn);
+               free((void *)a->ln);
 #endif
                a->sn = a->ln = NULL;
        }
        if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
-               if (a->data != NULL)
-                       free((void *)a->data);
+               free((void *)a->data);
                a->data = NULL;
                a->length = 0;
        }
Index: a_utctm.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/a_utctm.c,v
retrieving revision 1.21
diff -u -r1.21 a_utctm.c
--- a_utctm.c   19 Apr 2014 13:29:11 -0000      1.21
+++ a_utctm.c   21 Apr 2014 02:17:02 -0000
@@ -214,8 +214,7 @@
                        ASN1err(ASN1_F_ASN1_UTCTIME_ADJ, ERR_R_MALLOC_FAILURE);
                        return (NULL);
                }
-               if (s->data != NULL)
-                       free(s->data);
+               free(s->data);
                s->data = (unsigned char *)p;
        }
 
Index: ameth_lib.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/ameth_lib.c,v
retrieving revision 1.5
diff -u -r1.5 ameth_lib.c
--- ameth_lib.c 18 Apr 2014 13:26:34 -0000      1.5
+++ ameth_lib.c 21 Apr 2014 02:17:03 -0000
@@ -287,12 +287,10 @@
 {
        EVP_PKEY_ASN1_METHOD *ameth;
 
-       ameth = malloc(sizeof(EVP_PKEY_ASN1_METHOD));
+       ameth = calloc(1, sizeof(EVP_PKEY_ASN1_METHOD));
        if (!ameth)
                return NULL;
 
-       memset(ameth, 0, sizeof(EVP_PKEY_ASN1_METHOD));
-
        ameth->pkey_id = id;
        ameth->pkey_base_id = id;
        ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
@@ -382,10 +380,8 @@
 EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
 {
        if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
-               if (ameth->pem_str)
-                       free(ameth->pem_str);
-               if (ameth->info)
-                       free(ameth->info);
+               free(ameth->pem_str);
+               free(ameth->info);
                free(ameth);
        }
 }
Index: asn1_gen.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/asn1_gen.c,v
retrieving revision 1.6
diff -u -r1.6 asn1_gen.c
--- asn1_gen.c  19 Apr 2014 10:54:26 -0000      1.6
+++ asn1_gen.c  21 Apr 2014 02:17:03 -0000
@@ -258,10 +258,8 @@
        ret = d2i_ASN1_TYPE(NULL, &cp, len);
 
 err:
-       if (orig_der)
-               free(orig_der);
-       if (new_der)
-               free(new_der);
+       free(orig_der);
+       free(new_der);
 
        return ret;
 }
@@ -479,8 +477,7 @@
        der = NULL;
 
 bad:
-       if (der)
-               free(der);
+       free(der);
        if (sk)
                sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
        if (sect)
Index: asn_mime.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/asn_mime.c,v
retrieving revision 1.11
diff -u -r1.11 asn_mime.c
--- asn_mime.c  19 Apr 2014 07:20:59 -0000      1.11
+++ asn_mime.c  21 Apr 2014 02:17:03 -0000
@@ -945,10 +945,8 @@
 static void
 mime_hdr_free(MIME_HEADER *hdr)
 {
-       if (hdr->name)
-               free(hdr->name);
-       if (hdr->value)
-               free(hdr->value);
+       free(hdr->name);
+       free(hdr->value);
        if (hdr->params)
                sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
        free(hdr);
@@ -957,10 +955,8 @@
 static void
 mime_param_free(MIME_PARAM *param)
 {
-       if (param->param_name)
-               free(param->param_name);
-       if (param->param_value)
-               free(param->param_value);
+       free(param->param_name);
+       free(param->param_value);
        free(param);
 }
 
Index: asn_pack.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/asn_pack.c,v
retrieving revision 1.9
diff -u -r1.9 asn_pack.c
--- asn_pack.c  19 Apr 2014 07:20:59 -0000      1.9
+++ asn_pack.c  21 Apr 2014 02:17:03 -0000
@@ -77,10 +77,8 @@
        } else
                octmp = *oct;
 
-       if (octmp->data) {
-               free(octmp->data);
-               octmp->data = NULL;
-       }
+       free(octmp->data);
+       octmp->data = NULL;
 
        if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
                ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
Index: bio_asn1.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/bio_asn1.c,v
retrieving revision 1.6
diff -u -r1.6 bio_asn1.c
--- bio_asn1.c  19 Apr 2014 07:20:59 -0000      1.6
+++ bio_asn1.c  21 Apr 2014 02:17:03 -0000
@@ -186,8 +186,7 @@
        ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
        if (ctx == NULL)
                return 0;
-       if (ctx->buf)
-               free(ctx->buf);
+       free(ctx->buf);
        free(ctx);
        b->init = 0;
        b->ptr = NULL;
Index: bio_ndef.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/bio_ndef.c,v
retrieving revision 1.5
diff -u -r1.5 bio_ndef.c
--- bio_ndef.c  19 Apr 2014 07:20:59 -0000      1.5
+++ bio_ndef.c  21 Apr 2014 02:17:03 -0000
@@ -146,8 +146,7 @@
 err:
        if (asn_bio)
                BIO_free(asn_bio);
-       if (ndef_aux)
-               free(ndef_aux);
+       free(ndef_aux);
        return NULL;
 }
 
@@ -187,9 +186,7 @@
 
        ndef_aux = *(NDEF_SUPPORT **)parg;
 
-       if (ndef_aux->derbuf)
-               free(ndef_aux->derbuf);
-
+       free(ndef_aux->derbuf);
        ndef_aux->derbuf = NULL;
        *pbuf = NULL;
        *plen = 0;
Index: t_x509.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/t_x509.c,v
retrieving revision 1.18
diff -u -r1.18 t_x509.c
--- t_x509.c    18 Apr 2014 13:14:31 -0000      1.18
+++ t_x509.c    21 Apr 2014 02:17:04 -0000
@@ -247,8 +247,7 @@
        ret = 1;
 
 err:
-       if (m != NULL)
-               free(m);
+       free(m);
        return (ret);
 }
 
@@ -296,8 +295,7 @@
        return (1);
 
 err:
-       if (der != NULL)
-               free(der);
+       free(der);
        return (0);
 }
 
Index: tasn_dec.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/tasn_dec.c,v
retrieving revision 1.20
diff -u -r1.20 tasn_dec.c
--- tasn_dec.c  19 Apr 2014 17:40:49 -0000      1.20
+++ tasn_dec.c  21 Apr 2014 02:17:04 -0000
@@ -832,7 +832,7 @@
        ret = 1;
 
 err:
-       if (free_cont && buf.data)
+       if (free_cont)
                free(buf.data);
        return ret;
 }
@@ -955,8 +955,7 @@
                }
                /* If we've already allocated a buffer use it */
                if (*free_cont) {
-                       if (stmp->data)
-                               free(stmp->data);
+                       free(stmp->data);
                        stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
                        stmp->length = len;
                        *free_cont = 0;
Index: tasn_utl.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/tasn_utl.c,v
retrieving revision 1.8
diff -u -r1.8 tasn_utl.c
--- tasn_utl.c  18 Apr 2014 12:15:48 -0000      1.8
+++ tasn_utl.c  21 Apr 2014 02:17:04 -0000
@@ -153,8 +153,7 @@
 
        enc = asn1_get_enc_ptr(pval, it);
        if (enc) {
-               if (enc->enc)
-                       free(enc->enc);
+               free(enc->enc);
                enc->enc = NULL;
                enc->len = 0;
                enc->modified = 1;
@@ -171,8 +170,7 @@
        if (!enc)
                return 1;
 
-       if (enc->enc)
-               free(enc->enc);
+       free(enc->enc);
        enc->enc = malloc(inlen);
        if (!enc->enc)
                return 0;
Index: x_info.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/x_info.c,v
retrieving revision 1.10
diff -u -r1.10 x_info.c
--- x_info.c    18 Apr 2014 11:20:32 -0000      1.10
+++ x_info.c    21 Apr 2014 02:17:04 -0000
@@ -102,8 +102,7 @@
                X509_CRL_free(x->crl);
        if (x->x_pkey != NULL)
                X509_PKEY_free(x->x_pkey);
-       if (x->enc_data != NULL)
-               free(x->enc_data);
+       free(x->enc_data);
        free(x);
 }
 
Index: x_name.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/x_name.c,v
retrieving revision 1.15
diff -u -r1.15 x_name.c
--- x_name.c    18 Apr 2014 11:20:32 -0000      1.15
+++ x_name.c    21 Apr 2014 02:17:04 -0000
@@ -164,8 +164,7 @@
 
        BUF_MEM_free(a->bytes);
        sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free);
-       if (a->canon_enc)
-               free(a->canon_enc);
+       free(a->canon_enc);
        free(a);
        *pval = NULL;
 }
@@ -347,10 +346,9 @@
        X509_NAME_ENTRY *entry, *tmpentry = NULL;
        int i, set = -1, ret = 0;
 
-       if (a->canon_enc) {
-               free(a->canon_enc);
-               a->canon_enc = NULL;
-       }
+       free(a->canon_enc);
+       a->canon_enc = NULL;
+
        /* Special case: empty X509_NAME => null encoding */
        if (sk_X509_NAME_ENTRY_num(a->entries) == 0) {
                a->canon_enclen = 0;
Index: x_x509.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/x_x509.c,v
retrieving revision 1.12
diff -u -r1.12 x_x509.c
--- x_x509.c    18 Apr 2014 11:20:32 -0000      1.12
+++ x_x509.c    21 Apr 2014 02:20:20 -0000
@@ -105,8 +105,7 @@
                break;
 
        case ASN1_OP_D2I_POST:
-               if (ret->name != NULL)
-                       free(ret->name);
+               free(ret->name);
                ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
                break;
 
@@ -123,8 +122,8 @@
                sk_IPAddressFamily_pop_free(ret->rfc3779_addr, 
IPAddressFamily_free);
                ASIdentifiers_free(ret->rfc3779_asid);
 #endif
-               if (ret->name != NULL)
-                       free(ret->name);
+               free(ret->name);
+               ret->name = NULL;
                break;
        }
 
Index: n_pkey.c
===================================================================
RCS file: /cvs/src/lib/libssl/src/crypto/asn1/n_pkey.c,v
retrieving revision 1.14
diff -u -r1.14 n_pkey.c
--- n_pkey.c    19 Apr 2014 06:15:56 -0000      1.14
+++ n_pkey.c    21 Apr 2014 02:19:38 -0000
@@ -205,7 +205,7 @@
 
        if (!EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, buf, i,1, key, NULL))
                goto err;
-       OPENSSL_cleanse(buf, 256);
+       OPENSSL_cleanse(buf, sizeof(buf));
 
        /* Encrypt private key in place */
        zz = enckey->enckey->digest->data;
@@ -302,7 +302,7 @@
 
        if (!EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, buf, i,1, key, NULL))
                goto err;
-       OPENSSL_cleanse(buf, 256);
+       OPENSSL_cleanse(buf, sizeof(buf));
 
        if (!EVP_DecryptInit_ex(&ctx, EVP_rc4(), NULL, key, NULL))
                goto err;

Reply via email to