strftime_l lib64/libc.so.6中的SIGSEGV分段错误

lea*_*ode 4 unix linux

我将pro*c代码从UNIX移植到LINUX.代码被成功编译和创建可执行文件.但在运行时它的提升分段错误.我一步一步地调试了代码,下面是GDB调试的输出.

 Breakpoint 4 at 0x3b19690f50
 (gdb) n
 525             strftime (buf, MAX_STRING_LEN, "%d/%b/%Y:%H:%M:%S", dummy_time);
 (gdb) n

 Breakpoint 4, 0x0000003b19690f50 in strftime () from /lib64/libc.so.6
 (gdb) n
 Single stepping until exit from function strftime,
 which has no line number information.
 0x0000003b19690f70 in strftime_l () from /lib64/libc.so.6
 (gdb) n
 Single stepping until exit from function strftime_l,
 which has no line number information.

 Program received signal SIGSEGV, Segmentation fault.
 0x0000003b19690f8b in strftime_l () from /lib64/libc.so.6
Run Code Online (Sandbox Code Playgroud)

实际上在代码中调用函数strftime().但我不知道为什么它会进入strftime_l()/lib64/libc.so.6.

这个问题不会出现在UNIX中.请帮忙.代码是

static void speed_hack_libs(void)
{
    time_t dummy_time_t = time(NULL);
    struct tm *dummy_time = localtime (&dummy_time_t);
    struct tm *other_dummy_time = gmtime (&dummy_time_t);
    char buf[MAX_STRING_LEN];
    strftime (buf, MAX_STRING_LEN, "%d/%b/%Y:%H:%M:%S", dummy_time);
}
Run Code Online (Sandbox Code Playgroud)

jpa*_*cek 5

struct tm *dummy_time = localtime (&dummy_time_t);
struct tm *other_dummy_time = gmtime (&dummy_time_t);
Run Code Online (Sandbox Code Playgroud)

这不会奏效.从手册页:

localtime()函数将日历时间timep转换为细分时间表示,相对于用户指定的时区表示.... 返回值指向静态分配的结构,可能会被后续调用任何日期和时间函数覆盖.

gmtime()函数将日历时间timep转换为细分时间表示,以协调世界时(UTC)表示. 当年份不适合整数时,它可能返回NULL. 返回值指向静态分配的结构,该结构可能被后续调用任何日期和时间函数覆盖.

因此,*dummy_time可能会在您使用它时被覆盖,并包含不可预测的垃圾.您应该将数据复制到缓冲区,如下所示:

struct tm dummy_time ;
memcpy(&dummy_time, localtime (&dummy_time_t), sizeof(struct tm));
Run Code Online (Sandbox Code Playgroud)

虽然我不确定这会导致一个SIGSEGV(可能是获得月份名称等等 - 检查问题是否仍然存在LC_ALL=C),但您必须先解决这个问题才能继续.另外,检查(在调试器中)的内容*dummy_time.