Add safe wrappers to automatically free mbedtls types#1974
Merged
Conversation
|
mbedtls_safe_wrappers@16233 aka 20201203.11 vs master ewma over 50 builds from 15487 to 16228 |
achamayou
approved these changes
Dec 2, 2020
jumaffre
approved these changes
Dec 3, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

To try and stop the general class of issues described in #738, we need to support 2 slightly different things:
_free()if we've called_init(), for mbedtls structs. We could do this with a non-pointer auto-freeing wrapper struct.The latter could be done with a pattern like
try {} catch { X.reset(); }, but I think this approach is cleaner. Constructors populate local temporary structs, and only retain those (move-assigning them into members) at the end of the constructor, if it was successful. The wrapper type is aunique_ptrbecause it gives us the custom deleter hook and move-only semantics already, but we could write our own instead to remove the additional pointer indirection.The wrapper is generated by a macro, producing a
unique_ptrwith a custom deleter but also amake_uniquefunction that will call_init, for symmetry and safety. The interesting file ismbedtls_wrappers.h, the rest is just renames (mbedtls_foo=>mbedtls::Foo,mbedtls_bar(&foo...)=>mbedtls_bar(foo.get()...)).