Skip to content

Instantly share code, notes, and snippets.

@ricejasonf
Last active July 21, 2016 22:55
Show Gist options
  • Save ricejasonf/e1011544d14bf2978f1e58c95e6b8902 to your computer and use it in GitHub Desktop.
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.
#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