On 5/3/2013 4:57 AM, Tom marchand wrote:
Good eye but that's not it.  The code below should read BN_free(Res).
Thanks.


On May 2, 2013, at 10:42 PM, Jeremy Farrell wrote:

From: Tom marchand [mailto:tpmarch...@gmail.com]
Sent: Friday, May 03, 2013 2:55 AM

I am using the following code to create a temporary BIGNUM  to hold
the result of multiplication:

BIGNUM    *Res;

while(!Done)
{
    Res=BN_new();
    BN_init(Res);

    BN_mul(Res,A,B,Ctx);

    BN_free();

}

This code works with the exception that BN_free() is not releasing the
memory allocated to Res.  I already have a workaround but I am
wondering why the memory isn't being released. In production the code
runs for hours and is causing a significant memory leak.  I am I
misusing BN_new and BN_free? The code is running on OSX 10.5.8,
Openssl 0.9.7l.  I get the same result with OpenSSL 1.0.1e.

BN_free(Res);  ???

Remove the call to BN_init(Res).

BN_new() sets a flag in Res that says that BN_free(Res) should free
both the digit buffer at Res->d and Res itself.

BN_Init() sets the flag that says that BN_free(Res) should free only
Res->d.

The intended usage is EITHER:

void foo(const BIGNUM* A, const BIGNUM* B)
{
   BIGNUM    *Res;
   BN_CTX    *Ctx = BN_CTX_new();

   while(!Done)
   {
       Res=BN_new();

       BN_mul(Res,A,B,Ctx);

       BN_free_clear(Res);
   }

   BN_CTX_free(Ctx)
}

OR

void bar(const BIGNUM* A, const BIGNUM* B)
{
   BIGNUM    Res;
   BN_CTX    Ctx;

   BN_CTX_init(&Ctx);

   while(!Done)
   {
       BN_init(&Res);

       BN_mul(&Res,A,B,&Ctx);

       BN_free_clear(&Res);
   }

   BN_CTX_free(&Ctx)
}

The choice between the two styles is a matter of optimization specific to the compiler you use and the actual variable lifetimes in you application.


Enjoy

Jakob
--
Jakob Bohm, CIO, Partner, WiseMo A/S.  http://www.wisemo.com
Transformervej 29, 2730 Herlev, Denmark.  Direct +45 31 13 16 10
This public discussion message is non-binding and may contain errors.
WiseMo - Remote Service Management for PCs, Phones and Embedded
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to