Last active
July 21, 2016 22:55
-
-
Save ricejasonf/e1011544d14bf2978f1e58c95e6b8902 to your computer and use it in GitHub Desktop.
Invalid free when aligned_union is std::swap'd containing small std::string.
This file contains 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
#include <string> | |
#include <type_traits> | |
#include <utility> | |
struct my_empty { }; | |
using storage = std::aligned_union_t<sizeof(my_empty), my_empty, std::string>; | |
int main() | |
{ | |
storage x, y; | |
void* x_ptr = &x; | |
void* y_ptr = &y; | |
new (x_ptr) std::string("foo"); | |
std::swap(x, y); | |
using T = std::string; | |
reinterpret_cast<T*>(y_ptr)->~T(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/36965533/why-doesnt-stdswap-work-on-stdaligned-union