Hy, I am trying to encode an X509 certificate into base64, and since the encoder I'm using takes only char*, and typecasting doesn't work, i find a different way to do this. I'm sure there is a simpler way, but i just can't find it. Here is the code:

BUF_MEM *bptr;
BIO* mem = BIO_new(BIO_s_mem());
PEM_write_bio_X509(mem, x509p);
int i = BIO_get_mem_data(mem, &bptr);
_TCHAR* tmp = new _TCHAR( i+1 );
memcpy(tmp, bptr, i);
tmp[ i ] = _T('\0');			
		
BIO_free_all(mem);
X509_free(x509p);

The whole thing works "fine", but after this part every "new" object i create fails, and throws an std::bad_alloc exception. (I delete the tmp later on.

Here is the more exact part where it crashes:

void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
        {       // try to allocate size bytes
        void *p;
        while ((p = malloc(size)) == 0)
                if (_callnewh(size) == 0)
                {       // report no memory
                static const std::bad_alloc nomem;
                _RAISE(nomem);
                }

        return (p);
        }

I know it has something to do with memory leaks, but i can't find anything wrong with it, or I'm just missing it. The problem was caused only by this part of the code, if I remove it, everything is fine again.

Thank you for the help. (and Merry Christmas)

Recommended Answers

All 2 Replies

>_TCHAR* tmp = new _TCHAR( i+1 );
This looks like a bug to me. You want to simulate a dynamic array, but you're using the wrong syntax:

_TCHAR* tmp = new _TCHAR[i + 1];

Wow. I was working all day, I missed that... I'm sorry :)
Thanks for the REALLY fast answer.

Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.