boostorg / stacktrace

C++ library for storing and printing backtraces.
https://boost.org/libs/stacktrace
422 stars 70 forks source link

boost::stacktrace::to_string contains '\0' termination character in the returned string #81

Closed ghoben closed 4 years ago

ghoben commented 4 years ago

I'm using boost version 1_70 on windows.

In case the number of characters for a string representation of a frame exceeds 255 characters, the string returned contains the string termination character '\0' which makes the return string useless/errorful.

Simple call to: std::string backtrace = boost::stacktrace::to_string (boost::stacktrace::stacktrace ()); The root cause is in boost\stacktrace\detail\frame_msvc.ipp method

Starting with Line 200.

       char name[256];
        name[0] = '\0';
        ULONG size = 0;
        bool res = (S_OK == idebug_->GetNameByOffset(
            offset,
            name,
            sizeof(name),
            &size,
            0
        ));
        if (!res && size != 0) {
            result.resize(size);
            res = (S_OK == idebug_->GetNameByOffset(
                offset,
                &result[0],
                static_cast<ULONG>(result.size()),
                &size,
                0
            ));

In case the original buffer size for name (=256) is too small idebug_->GetNameByOffset() returns false, which calls the method again in L213 given the adjusted size. GetNameByOffset also copies the '\0' termination character into the std::string, which is the problem.

A simple bug fix would be to remove to '\0' termination character from the result after the call successfully completed by adding:

            if(res) {
               //The copied string is \0 terminated, we have to remove the \0.
               result.resize(size-1);
            }
ghoben commented 4 years ago

I see, this is fixed on the latest: boost 1_71

apolukhin commented 4 years ago

Yes, that was fixed in Boost 1.71