On Thu, Apr 27, 2023 at 1:57 PM Dwight Kulkarni <dwi...@realtime-7.com> wrote:
>
> Ok I think those RSA errors with memory are due to this pipelining, because 
> the b64decode(..) b64encode(..) is called every where, and if I understand 
> correctly, the string inside will be destroyed and I have to use the 
> redirector. Can you confirm if you agree with this code change and my 
> understanding of the problem ?
>
> Previous Code:
> std::string b64encode(std::string str)
> {
>    using namespace CryptoPP;
>
>     std::string encoded;
>     StringSource(str, true,
>         new Base64Encoder(
>            new StringSink(encoded),
>            false
>         )
>     );
>     return encoded; //encoded will be destroyed once StringSource(..) goes 
> out of scope
> }

This code is Ok.

The variable 'encoded' is not destroyed because it is a reference, and
not a pointer. 'encoded' is a stack variable. It will be destroyed
when the stack frame is cleaned up.

> New Code:
>
> std::string b64encode(std::string str)
> {
>    using namespace CryptoPP;
>
>     std::string encoded;
>     Base64Encoder df(new CryptoPP::StringSink(encoded),false);
>     StringSource(str, true, new Redirector(df) );
>     return encoded; //encoded won't be destroyed
> }

This code is Ok, too. This code is effectively the same as the first
example. The Base64Encoder is now an automatic variable, and it will
be cleaned up when the destructor runs during stack cleanup.

The variable 'encoded' is not destroyed because it is a reference, and
not a pointer. 'encoded' is a stack variable. It will be destroyed
when the stack frame is cleaned up.

Your memory error lies elsewhere. Run Asan over your program.

Jeff

-- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/cryptopp-users/CAH8yC8m%2B8_ARwU6ZSfQcpW-LQ_V%3DZVRvxJLJ4vzpKaiobzMBFQ%40mail.gmail.com.

Reply via email to