libnitrokey icon indicating copy to clipboard operation
libnitrokey copied to clipboard

Do not return local variable address for TargetBufferSmallerThanSource message

Open szszszsz opened this issue 1 year ago • 2 comments

Use static string object for keeping the c_str message for the caller. Strings collection used as an alternative to memory leaks done via strdup(). To add, TargetBufferSmallerThanSource Exception should never happen in a correctly written library client, as this completely depends on the implementation and not on the communication with the device

Downside: if missed and when occurring too often, the memory taken by the exception messages can take too much memory. Potential improvement: replace std::vector with a kind of a ring buffer, or add simple wrapping logic over it.

Fixes #214

Edit: this should avoid breaking ABI by using static memory. Note: there might be multiple exceptions' strings collections (for each compilation unit including this header). Edit: correct that for libnitrokey 4, by keeping string as a member of the exception.

szszszsz avatar Aug 29 '22 16:08 szszszsz

@monwarez Can you take a brief look?

szszszsz avatar Aug 29 '22 16:08 szszszsz

Seems good to me, since the exception message is a view, a simple fix for the downside would be to simply reset the vector before adding the message.

monwarez avatar Aug 30 '22 15:08 monwarez