unix系统调用getenv() 出core
有一个获取当前时间的函数,里面有调用获取当地时区时间函数localtime, localtime在调用到getenv函数时出现core, 不是第一次调用的时候出core, 而是在连续很快的调用情况下会出现core。 大家帮忙看看什么原因。
void log1(const char *format, ...) //写日志
{
struct timeval now;
struct tm* ts;
gettimeofday(&now, 0);
ts = localtime(&now.tv_sec);
if (ts == NULL)
{
printf("localtime : %d, %s\n", errno, strerror(errno));
return ;
}
char dateinfo[64];
memset(dateinfo, 0 ,sizeof(dateinfo));
sprintf(dateinfo, "%02d%02d%02d%02d", ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec);
fprintf(os_log, "[%s:%06ld]", dateinfo, now.tv_usec);
va_list argp;
va_start (argp, format);
vfprintf(os_log, format, argp);
va_end (argp);
}
出现的core信息:
Program terminated with signal 11, Segmentation fault.
SEGV_MAPERR - Address not mapped to object
#0 0xc000000000338420:0 in getenv+0x1160 () from /usr/lib/hpux64/libc.so.1
(gdb) where
#0 0xc000000000338420:0 in getenv+0x1160 () from /usr/lib/hpux64/libc.so.1
#1 0xc000000000333a50:0 in tzset+0x110 () from /usr/lib/hpux64/libc.so.1
#2 0xc000000000330510:0 in localtime_r+0x120 () from /usr/lib/hpux64/libc.so.1
#3 0xc0000000003301d0:0 in localtime+0x30 () from /usr/lib/hpux64/libc.so.1
#4 0x40000000000695e0:0 in log1 (this=0x6000000000000b30,
format=0x40000000000143e0 "[smpp] get resp failed\n") at r5log.cpp:203