diff options
author | Chris Wilson <chris+github@qwirx.com> | 2015-08-31 22:36:45 +0100 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2015-08-31 22:57:18 +0100 |
commit | 152dc613483672b8535c9095e24bf0d18104ec45 (patch) | |
tree | f77ad1725f2279fd7516cabb492ab856dc83993b | |
parent | 12ceb036f826c4796a47397f0c24aa3bdcf4ed42 (diff) |
Fix exception message corruption.
Using the c_str() of a std::string after the string has been freed is unsafe
and can lead to corrupted error messages in tests, or worse.
-rwxr-xr-x | lib/common/makeexception.pl.in | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/lib/common/makeexception.pl.in b/lib/common/makeexception.pl.in index e4018180..498c81b8 100755 --- a/lib/common/makeexception.pl.in +++ b/lib/common/makeexception.pl.in @@ -117,6 +117,7 @@ print H <<__E; private: unsigned int mSubType; std::string mMessage; + std::string mWhat; }; #endif // $guardname @@ -190,21 +191,15 @@ unsigned int ${class}Exception::GetSubType() const throw() const char * ${class}Exception::what() const throw() { - std::string what = "${class}"; - -#ifdef EXCEPTION_CODENAMES_EXTENDED - if(mSubType < (sizeof(whats) / sizeof(whats[0]))) - { - what = whats[mSubType]; - } -#endif + // We need to use a string that will persist after this function exits. + mWhat = GetMessage(mSubType); if(mMessage != "") { - what += ": " + mMessage; + mWhat += ": " + mMessage; } - return what.c_str(); + return mWhat.c_str(); } const char * ${class}Exception::GetMessage(int SubType) |