Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Logging in a static function causes segfault #53

@godbyk

Description

@godbyk

Previously, logging from a static function used to work fine. It would just log to the console because InitGoogleLogging() hadn't been called yet.

Now, logging from a static function causes the program to segfault.

Here's a short test program that demonstrates the problem. The static bool b = f("..."); line triggers the crash. If that line is commented out, the program runs fine.

#include <glog/logging.h>
#include <string>

bool f(const std::string& message)
{
    LOG(INFO) << message;
    return true;
}

// Uncommenting the following line will trigger the crash
//static bool b = f("Logging before main() crashes.");

int main(int argc, char* argv[])
{
    f("Logging after main() but before InitGoogleLogging() is fine.");
    google::InitGoogleLogging(argv[0]);
    f("Logging after InitGoogleLogging() is obviously fine.");

    return 0;
}

Example stack trace from gdb:

#0  0x00007ffff796eae0 in std::string::empty() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x0000000000408366 in google::LogMessage::Init (this=0x7fffffffd458, file=0x42d484 "static_log.cpp", line=6, severity=0, 
    send_method=(void (google::LogMessage::*)(google::LogMessage * const)) 0x4087f0 <google::LogMessage::SendToLog()>)
    at /home/godbyk/git/glog/src/logging.cc:1251
#2  0x0000000000407b79 in google::LogMessage::LogMessage (this=0x7fffffffd458, file=0x42d484 "static_log.cpp", line=6)
    at /home/godbyk/git/glog/src/logging.cc:1160
#3  0x0000000000405925 in f (message="Logging before main() crashes.") at static_log.cpp:6
#4  0x000000000040516f in __cxx_global_var_init () at static_log.cpp:11
#5  0x00000000004051fe in _GLOBAL__sub_I_static_log.cpp ()
#6  0x000000000042d3ed in __libc_csu_init ()
#7  0x00007ffff6fe49cf in __libc_start_main (main=0x4059a0 <main(int, char**)>, argc=1, argv=0x7fffffffd5e8, 
    init=0x42d3a0 <__libc_csu_init>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffd5d8) at libc-start.c:245
#8  0x00000000004057d9 in _start ()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions