ctime()导致SIGABRT(?)

ctime()导致SIGABRT(?),c,sigabrt,ctime,C,Sigabrt,Ctime,代码如下: void i_log_ (int error, const char * file, int line, const char * fmt, ...) { /* Get error description */ char * str_err = get_str_error (errno); remove_trailing_newl (str_err); /* Format string and parameters */ char

代码如下:

void i_log_ (int error, const char * file, int line, const char * fmt, ...)
{
     /* Get error description */
     char * str_err = get_str_error (errno);
     remove_trailing_newl (str_err);

     /* Format string and parameters */
     char message [1024];
     va_list ap;
     va_start (ap, fmt);
     vsprintf (message, fmt, ap);
     va_end (ap);

     /* Get time */
     time_t t = time (NULL);
     char stime [64];
     char * temp = ctime (&t);
     strncpy (stime, temp, sizeof stime - 1);
     remove_trailing_newl (stime);

     FILE * log;


#ifdef __WIN32__
#else
# ifdef P_LISTENER
     log = fopen (I_LOG_FILE, "a+b");
     flock (fileno (log), LOCK_EX);
# else /* shared file descriptor of log, lock before opening */
     pthread_mutex_lock (& mutex);
     log = fopen (I_LOG_FILE, "a+b");
# endif
#endif

     if (log) {
          if (error)
               fprintf (log, ERR_FORMAT, stime, file, line, str_err, message);
          else
               fprintf (log, ERR_FORMAT_NO_ERRNO, stime, file, line, message);
     }

#ifdef __WIN32__
     free (str_err);
#else
# ifdef P_LISTENER
     flock (fileno (log), LOCK_UN);
     fclose (log);
# else
     fclose (log);
     pthread_mutex_unlock (& mutex);
# endif
#endif
     return;
}
虽然有一个锁机制,但在这种情况下,函数不是并发调用的,所以我认为这不是问题所在。但是,程序会收到一个
SIGABRT

[...]
(gdb) c
Continuing.

Program received signal SIGHUP, Hangup. // It's OK, I sent this.
0x00dee416 in __kernel_vsyscall ()
(gdb) c
Continuing.

Program received signal SIGABRT, Aborted.
0x00dee416 in __kernel_vsyscall ()
(gdb) up
#1  0x0013ae71 in raise () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#2  0x0013e34e in abort () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#3  0x00171577 in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#4  0x0017b961 in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#5  0x0017d28b in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#6  0x0018041d in free () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#7  0x0019b0d2 in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#8  0x0019b3c5 in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#9  0x00199a9f in localtime () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#10 0x00199951 in ctime () from /lib/i386-linux-gnu/libc.so.6
(gdb) up
#11 0x08049634 in i_log_ (error=0, file=0x804b17d "src/group.c", line=53, fmt=0x804b128 "Setting up new configuration: listener type: %s, number: %d, http-log: %s, port: %d.") at src/error.c:42
42       char * temp = ctime (&t);
(gdb) print temp
$1 = 0x260000 ""
(gdb) print t
$2 = 1329935482
(gdb) print &t
$3 = (time_t *) 0xbff8a5b8
(gdb) 
我一点也不知道
ctime
返回的是一个空字符串,手册页没有提到这种情况。仔细想想,我不明白为什么它会返回一个空字符串,以及代码有什么问题


非常感谢您的帮助。

由于崩溃发生在
ctime()
内部,并且您传递的指针是有效的,因此问题可能是您已经超出了内存限制(堆栈跟踪中有
free()
)在其他地方,问题只在这里表现出来。

由于崩溃发生在
ctime()
内部,并且您传递的指针是有效的,所以问题可能是您已经超出了内存的界限(堆栈跟踪中有
free()
)在其他地方,问题只在这里显现。

ctime
不会返回空字符串。它还没有回来,因为它在做它的事情时崩溃了


崩溃发生在
free()。如果您在受支持的平台上运行,请尝试使用Valgrind之类的工具检查内存访问。

ctime
不会返回空字符串。它还没有回来,因为它在做它的事情时崩溃了


崩溃发生在
free()。如果您在受支持的平台上运行,请尝试使用Valgrind之类的工具检查内存访问。

这可能是对
free()
进行的(失败的)健全性检查。我希望在stderr上看到一些输出(类似于“glibc-detected-double-free”或类似的东西)。这可能是对
free()
的(失败的)健全性检查。我希望在stderr上看到一些输出(类似于“glibc-detected-double-free”或类似的东西)。您可能已经在别处调用了
ctime()。这可能是通过使用
ctime\u r()
解决的。您可能已经在别处调用了
ctime()
。这可能是通过使用
ctime\u r()
解决的。是的,我用Memcheck(一个buf[sizeof buf]='\0',duh)纠正了一个与崩溃发生位置相去甚远的错误。我不明白它是怎么联系起来的,但仅此而已。还感谢您向我指出,ctime实际上没有返回。像这样的细微内存溢出会破坏
malloc()
(和
free()
)内部使用的数据结构,从而导致以后内存分配函数崩溃。一旦发生这种情况,遇到损坏只是时间问题。是的,我用Memcheck(一个buf[sizeof buf]='\0',duh)纠正了一个与崩溃发生地相去甚远的bug。我不明白它是怎么联系起来的,但仅此而已。还感谢您向我指出,ctime实际上没有返回。像这样的细微内存溢出会破坏
malloc()
(和
free()
)内部使用的数据结构,从而导致以后内存分配函数崩溃。一旦发生这种情况,遇到腐败只是时间问题。