OTEL_INTERNAL_LOG_DISPATCH Macro's Temporary String's c_str Method Causes Core Dump #2393
Labels
bug
Something isn't working
triage/not-reproducible
Indicates an issue can not be reproduced as described.
I have defined my own LogHandler, which inherits from ::opentelemetry::sdk::common::internal_log::LogHandler. I've encountered a core dump in my service:
I suspect it might be related to a potential issue with the OTEL_INTERNAL_LOG_DISPATCH macro in the following code snippet:
This code can be found at https://github.com/open-telemetry/opentelemetry-cpp/blob/main/sdk/include/opentelemetry/sdk/common/global_log_handler.h#L157C52-L157C68.
In this macro, tmp_stream.str().c_str() returns a pointer to a temporary string. This temporary string is the return value of tmp_stream.str(), which will be destructed at the end of the current statement. Therefore, it is unsafe to continue using this pointer after the statement ends.
If the log_handler->Handle function needs to access this string after the current statement, this macro may cause issues. One possible solution is to store the return value of tmp_stream.str() in a variable and pass the c_str() of this variable to the Handle function. For example:
std::string tmp_string = tmp_stream.str(); log_handler->Handle(level, __FILE__, __LINE__, tmp_string.c_str(), attributes);
In this modified code, tmp_string will be destructed at the end of the do...while block, so there will be no issues as long as the Handle function finishes using the string before that.
If my analysis is correct, I will submit a PR to fix this issue.
The text was updated successfully, but these errors were encountered: