Included are patches for OpenSSL 0.9.8j

The patches have been generated using automated tests from running
Coccinelle (http://www.emn.fr/x-info/coccinelle/) on the OpenSSL
codebase.

As far as I can tell, all the cases found are genuine optimizations,
not exactly bugs, but things that ought to be changed in the code. I
am no C or OpenSSL expert, but I believe that the attached patches
would be beneficial.

The files are described below:

badzero.patch
To improve code readability, it seems better not to compare a
pointer-typed value to 0. This patch changes types of (pointertype
== 0) to (pointertype == NULL), and with != as well. This is of
course a matter of taste, but it seems like the majority of pointer
comparisons are done with NULL instead of 0 in the code after all.

notnull.patch
NULL tests on already tested values are removed with this patch, as
well as redundant NULL checks. For instance, if a pointer is checked
for NULL, and afterwards an error occurs, and execution moves to an
error label, there are several cases where the pointer again is
checked for NULL, which could never occur in the given code path.
Also I have found a couple of repeated code lines, where the
redundant lines are removed with this patch.

Best regards,
Sune Rievers
diff -u -p a/crypto/bio/b_print.c b/crypto/bio/b_print.c
--- a/crypto/bio/b_print.c 2007-09-15 19:05:57.000000000 +0200
+++ b/crypto/bio/b_print.c 2009-03-04 22:32:52.000000000 +0100
@@ -443,7 +443,7 @@ fmtstr(
     int padlen, strln;
     int cnt = 0;

-    if (value == 0)
+    if (value == NULL)
         value = "<NULL>";
     for (strln = 0; value[strln]; ++strln)
         ;
diff -u -p a/crypto/mem.c b/crypto/mem.c
--- a/crypto/mem.c 2008-11-24 18:02:49.000000000 +0100
+++ b/crypto/mem.c 2009-03-04 22:32:49.000000000 +0100
@@ -142,7 +142,7 @@ int CRYPTO_set_mem_functions(void *(*m)(
        {
        if (!allow_customize)
                return 0;
-       if ((m == 0) || (r == 0) || (f == 0))
+       if ((m == NULL) || (r == NULL) || (f == NULL))
                return 0;
        malloc_func=m; malloc_ex_func=default_malloc_ex;
        realloc_func=r; realloc_ex_func=default_realloc_ex;
@@ -159,7 +159,7 @@ int CRYPTO_set_mem_ex_functions(
        {
        if (!allow_customize)
                return 0;
-       if ((m == 0) || (r == 0) || (f == 0))
+       if ((m == NULL) || (r == NULL) || (f == NULL))
                return 0;
        malloc_func=0; malloc_ex_func=m;
        realloc_func=0; realloc_ex_func=r;
diff -u -p a/crypto/sha/sha512.c b/crypto/sha/sha512.c
--- a/crypto/sha/sha512.c 2008-09-16 12:47:28.000000000 +0200
+++ b/crypto/sha/sha512.c 2009-03-04 22:32:51.000000000 +0100
@@ -140,7 +140,7 @@ int SHA512_Final (unsigned char *md, SHA

        sha512_block_data_order (c,p,1);

-       if (md==0) return 0;
+       if (md==NULL) return 0;

        switch (c->md_len)
                {
diff -u -p a/crypto/ecdh/ech_ossl.c b/crypto/ecdh/ech_ossl.c
--- a/crypto/ecdh/ech_ossl.c 2005-05-16 12:11:00.000000000 +0200
+++ b/crypto/ecdh/ech_ossl.c 2009-03-04 22:32:52.000000000 +0100
@@ -186,7 +186,7 @@ static int ecdh_compute_key(void *out, s
                goto err;
                }

-       if (KDF != 0)
+       if (KDF != NULL)
                {
                if (KDF(buf, buflen, out, &outlen) == NULL)
                        {
diff -u -p a/fips/rsa/fips_rsa_gen.c b/fips/rsa/fips_rsa_gen.c
--- a/fips/rsa/fips_rsa_gen.c 2008-09-16 12:12:21.000000000 +0200
+++ b/fips/rsa/fips_rsa_gen.c 2009-03-02 16:29:49.000000000 +0100
@@ -122,8 +122,6 @@ int fips_check_rsa(RSA *rsa)

        ret = 1;

-       if (!ptbuf)
-               goto err;

        err:
        if (ret == 0)
diff -u -p a/test/methtest.c b/test/methtest.c
--- a/test/methtest.c 2002-11-28 19:54:30.000000000 +0100
+++ b/test/methtest.c 2009-03-02 16:30:28.000000000 +0100
@@ -73,7 +73,6 @@ char *argv[];
        if (top == NULL) goto err;

        tmp1=METH_new(x509_by_file());
-       if (top == NULL) goto err;
        METH_arg(tmp1,METH_TYPE_FILE,"cafile1");
        METH_arg(tmp1,METH_TYPE_FILE,"cafile2");
        METH_push(top,METH_X509_CA_BY_SUBJECT,tmp1);
diff -u -p a/crypto/x509v3/pcy_tree.c b/crypto/x509v3/pcy_tree.c
--- a/crypto/x509v3/pcy_tree.c 2008-11-05 19:36:50.000000000 +0100
+++ b/crypto/x509v3/pcy_tree.c 2009-03-02 16:32:08.000000000 +0100
@@ -160,11 +160,6 @@ static int tree_init(X509_POLICY_TREE **
        tree->auth_policies = NULL;
        tree->user_policies = NULL;

-       if (!tree)
-               {
-               OPENSSL_free(tree);
-               return 0;
-               }

        memset(tree->levels, 0, n * sizeof(X509_POLICY_LEVEL));

diff -u -p a/crypto/objects/obj_lib.c b/crypto/objects/obj_lib.c
--- a/crypto/objects/obj_lib.c 2006-02-08 20:16:18.000000000 +0100
+++ b/crypto/objects/obj_lib.c 2009-03-02 16:32:49.000000000 +0100
@@ -109,7 +109,6 @@ ASN1_OBJECT *OBJ_dup(const ASN1_OBJECT *
        return(r);
 err:
        OBJerr(OBJ_F_OBJ_DUP,ERR_R_MALLOC_FAILURE);
-       if (r != NULL)
                {
                if (ln != NULL) OPENSSL_free(ln);
                if (r->data != NULL) OPENSSL_free(r->data);
diff -u -p a/crypto/asn1/a_d2i_fp.c b/crypto/asn1/a_d2i_fp.c
--- a/crypto/asn1/a_d2i_fp.c 2005-05-09 02:27:32.000000000 +0200
+++ b/crypto/asn1/a_d2i_fp.c 2009-03-02 16:34:34.000000000 +0100
@@ -255,6 +255,6 @@ static int asn1_d2i_read_bio(BIO *in, BU
        *pb = b;
        return off;
 err:
-       if (b != NULL) BUF_MEM_free(b);
+        BUF_MEM_free(b);
        return(ret);
        }
diff -u -p a/crypto/dso/dso_lib.c b/crypto/dso/dso_lib.c
--- a/crypto/dso/dso_lib.c 2003-12-27 15:40:08.000000000 +0100
+++ b/crypto/dso/dso_lib.c 2009-03-02 16:29:40.000000000 +0100
@@ -400,8 +400,6 @@ char *DSO_merge(DSO *dso, const char *fi
                return(NULL);
                }
        if(filespec1 == NULL)
-               filespec1 = dso->filename;
-       if(filespec1 == NULL)
                {
                DSOerr(DSO_F_DSO_MERGE,DSO_R_NO_FILE_SPECIFICATION);
                return(NULL);
diff -u -p a/crypto/cms/cms_lib.c b/crypto/cms/cms_lib.c
--- a/crypto/cms/cms_lib.c 2008-04-06 17:57:44.000000000 +0200
+++ b/crypto/cms/cms_lib.c 2009-03-02 16:30:09.000000000 +0100
@@ -473,8 +473,6 @@ int CMS_add0_cert(CMS_ContentInfo *cms,
        pcerts = cms_get0_certificate_choices(cms);
        if (!pcerts)
                return 0;
-       if (!pcerts)
-               return 0;
        for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++)
                {
                cch = sk_CMS_CertificateChoices_value(*pcerts, i);
diff -u -p a/crypto/err/err_str.c b/crypto/err/err_str.c
--- a/crypto/err/err_str.c 2008-10-26 19:42:01.000000000 +0100
+++ b/crypto/err/err_str.c 2009-03-02 16:32:00.000000000 +0100
@@ -244,11 +244,6 @@ static void build_SYS_str_reasons(void)

        CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
        CRYPTO_w_lock(CRYPTO_LOCK_ERR);
-       if (!init)
-               {
-               CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
-               return;
-               }

        for (i = 1; i <= NUM_SYS_STR_REASONS; i++)
                {
diff -u -p a/ssl/s3_srvr.c b/ssl/s3_srvr.c
--- a/ssl/s3_srvr.c 2009-01-07 11:48:23.000000000 +0100
+++ b/ssl/s3_srvr.c 2009-03-02 16:33:04.000000000 +0100
@@ -1366,11 +1366,6 @@ int ssl3_send_server_key_exchange(SSL *s
                                }

                        /* Duplicate the ECDH structure. */
-                       if (ecdhp == NULL)
-                               {
-                               
SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
-                               goto err;
-                               }
                        if (!EC_KEY_up_ref(ecdhp))
                                {
                                
SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
diff -u -p a/apps/ecparam.c b/apps/ecparam.c
--- a/apps/ecparam.c 2005-05-31 19:31:50.000000000 +0200
+++ b/apps/ecparam.c 2009-03-02 16:40:53.000000000 +0100
@@ -464,8 +464,6 @@ bad:

        if (check)
                {
-               if (group == NULL)
-                       BIO_printf(bio_err, "no elliptic curve parameters\n");
                BIO_printf(bio_err, "checking elliptic curve parameters: ");
                if (!EC_GROUP_check(group, NULL))
                        {
diff -u -p a/apps/ca.c b/apps/ca.c
--- a/apps/ca.c 2008-01-03 23:53:03.000000000 +0100
+++ b/apps/ca.c 2009-03-02 16:48:45.000000000 +0100
@@ -2187,7 +2187,6 @@ err:
                X509_NAME_free(subject);
        if ((dn_subject != NULL) && !email_dn)
                X509_NAME_free(dn_subject);
-       if (tmptm != NULL)
                ASN1_UTCTIME_free(tmptm);
        if (ok <= 0)
                {

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to