I have a class that contains a a string. Currently, it’s an std::wstring but does not have to be. I had read here that std::string should not be used but I am wondering if something like this would work:
if (aString.length() == aString.capacity() )
{
std::wstring oldString = aString;
aString = wstring(aString);
aString.reserve(PREALLOCATION_AMOUNT);
SecureZeroMemory((PVOID)oldString.c_str(),oldString.size());
oldString.clear();
}
would this basically equate to a secure realloc of the string buffer? If not is there a better solution?
It’s not guaranteed that
c_str()will point to the original buffer and not make a copy. That’s probably the way it works, but there’s no way to be sure without looking at your specific implementation ofbasic_string.There are enough potential problems with making
std::wstringsecure that I’d avoid it entirely and find a secure string class or write my own.