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

Reply via email to