Thanks Steve,

After commenting out lines 24 & 25 there is still unfree'd memory:

Stuart


==8155== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==8155== malloc/free: in use at exit: 528 bytes in 10 blocks.
==8155== malloc/free: 137 allocs, 127 frees, 13,340 bytes allocated.
==8155== For counts of detected errors, rerun with: -v
==8155== searching for pointers to 10 not-freed blocks.
==8155== checked 227,584 bytes.
==8155==
==8155== 528 bytes in 10 blocks are still reachable in loss record 1 of 1
==8155==    at 0x4A05809: malloc (vg_replace_malloc.c:149)
==8155==    by 0x35156DAD51: CRYPTO_malloc (in /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x351567EAC8: lh_new (in /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x351565B4C4: (within /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x351565B5E5: (within /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x351565B98A: (within /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x351567698F: BIO_set (in /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x3515676A19: BIO_new (in /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x351569DE71: PEM_ASN1_read (in /lib64/libcrypto.so.0.9.8e)
==8155==    by 0x400836: main (rsatest.c:30)
==8155==
==8155== LEAK SUMMARY:
==8155==    definitely lost: 0 bytes in 0 blocks.
==8155==      possibly lost: 0 bytes in 0 blocks.
==8155==    still reachable: 528 bytes in 10 blocks.
==8155==         suppressed: 0 bytes in 0 blocks.

----- Original Message ----
From: Dr. Stephen Henson <st...@openssl.org>
To: openssl-users@openssl.org
Sent: Mon, April 19, 2010 3:12:53 PM
Subject: Re: possible user error / memory leak using RSA_new() and RSA_free();

On Mon, Apr 19, 2010, Stuart Weatherby wrote:

> Hi List,
> 
> I am trying to figure out why there is a memory leak using RSA_new & RSA_free:
> Below is a code sample (which will produce a memory leak) and the relevent 
> valgrind output. I have checked the documentation but I still fail to see my 
> error. 
> 
> As I understand the docuumentation, RSA_free() is the only required call to 
> free memory allocated using the RSA_new() function. 
> 
> Thanks,
> 
> Stuart
> 
> 
> int main (void)
> {
>    FILE *fp;
>    RSA  *p = NULL;
>    char *pt = "hi\0";
>    char pt_0 = *pt;
>    int pt_len = strlen(pt);
>    unsigned char *ct;
>    int ct_len = 0;
> 
>    if ((p = RSA_new()) == NULL)
>       return 1;
>    if ((fp = fopen ("pub.key", "rb")) == NULL)
>       return 2;
>    if ((p = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)) == NULL)
>    {
>       fclose(fp);
>       return 3;
>    }
>    fclose(fp);
>    ct = malloc(RSA_size(p));
>    if(RSA_public_encrypt(pt_len,(unsigned char *) pt,ct, p, 
> RSA_PKCS1_OAEP_PADDING) == -1)
>      return 4;
>    free(ct);
>    RSA_free(p);
>    return 0;
> }
> 

You are allocating an RSA structure then completely overwriting the pointer
with a new one. Leaving the original as the leak.

Delete the RSA_new() call.

Steve.
--
Dr Stephen N. Henson. OpenSSL project core developer.
Commercial tech support now available see: http://www.openssl.org
______________________________________________________________________
OpenSSL Project                                http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                          majord...@openssl.org



______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to