Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTEL_INTERNAL_LOG_DISPATCH Macro's Temporary String's c_str Method Causes Core Dump #2393

Closed
ninghejun opened this issue Nov 6, 2023 · 1 comment
Labels
bug Something isn't working triage/not-reproducible Indicates an issue can not be reproduced as described.

Comments

@ninghejun
Copy link
Contributor

I have defined my own LogHandler, which inherits from ::opentelemetry::sdk::common::internal_log::LogHandler. I've encountered a core dump in my service:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f6a063ff5e8 in std::ostream::sentry::~sentry() () from /lib64/libstdc++.so.6
[Current thread is 1 (Thread 0x7f69fb5ff700 (LWP 654))]
(gdb) bt
#0  0x00007f6a063ff5e8 in std::ostream::sentry::~sentry() () from /lib64/libstdc++.so.6
#1  0x00007f6a063ffd32 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) ()
   from /lib64/libstdc++.so.6
#2  0x0000000001d69d37 in opentelemetry::v1::exporter::otlp::OtlpHttpLogRecordExporter::Export(opentelemetry::v1::nostd::span<std::unique_ptr<opentelemetry::v1::sdk::logs::Recordable, std::default_delete<opentelemetry::v1::sdk::logs::Recordable> >, 18446744073709551615ul> const&) ()
#3  0x0000000001db0870 in opentelemetry::v1::sdk::logs::BatchLogRecordProcessor::Export() ()
#4  0x0000000001dafd4f in opentelemetry::v1::sdk::logs::BatchLogRecordProcessor::DoBackgroundWork() ()
#5  0x00007f6a063a6b73 in execute_native_thread_routine () from /lib64/libstdc++.so.6
#6  0x00007f6a04d1e2de in start_thread () from /lib64/libpthread.so.0
#7  0x00007f6a04837e83 in clone () from /lib64/libc.so.6
(gdb) 

I suspect it might be related to a potential issue with the OTEL_INTERNAL_LOG_DISPATCH macro in the following code snippet:

#define OTEL_INTERNAL_LOG_DISPATCH(level, message, attributes)                            \
  do                                                                                      \
  {                                                                                       \
    // ......
    std::stringstream tmp_stream;                                                         \
    tmp_stream << message;                                                                \
    log_handler->Handle(level, __FILE__, __LINE__, tmp_stream.str().c_str(), attributes); \
  } while (false);

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.

@ninghejun ninghejun added the bug Something isn't working label Nov 6, 2023
@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Nov 6, 2023
@ninghejun ninghejun changed the title Potential issue with OTEL_INTERNAL_LOG_DISPATCH macro and temporary string pointer causing a core dump OTEL_INTERNAL_LOG_DISPATCH Macro's Temporary String's c_str Method Causes Core Dump Nov 6, 2023
@ninghejun ninghejun changed the title OTEL_INTERNAL_LOG_DISPATCH Macro's Temporary String's c_str Method Causes Core Dump OTEL_INTERNAL_LOG_DISPATCH Macro's Temporary String's c_str() Method Causes Core Dump Nov 6, 2023
@ninghejun ninghejun changed the title OTEL_INTERNAL_LOG_DISPATCH Macro's Temporary String's c_str() Method Causes Core Dump OTEL_INTERNAL_LOG_DISPATCH Macro's Temporary String's c_str Method Causes Core Dump Nov 6, 2023
ninghejun added a commit to ninghejun/opentelemetry-cpp that referenced this issue Nov 6, 2023
ninghejun added a commit to ninghejun/opentelemetry-cpp that referenced this issue Nov 6, 2023
ninghejun added a commit to ninghejun/opentelemetry-cpp that referenced this issue Nov 6, 2023
@marcalff marcalff added triage/not-reproducible Indicates an issue can not be reproduced as described. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Nov 13, 2023
@marcalff
Copy link
Member

Closing, can not reproduce.

The code in OTEL_INTERNAL_LOG_DISPATCH() does not seem to be at fault here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage/not-reproducible Indicates an issue can not be reproduced as described.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants