【发布时间】:2013-07-14 12:51:09
【问题描述】:
我得到了以下代码(log 函数的一部分):
/* set to 32 on purpose */
#define MAX_LOG_MSG_SZ 32
void log(const char *fmt, ...) {
....
char msg[MAX_LOG_MSG_SZ] = {0};
int nb_bytes = 0;
/* get current time */
time_t now = time(NULL);
char time_buf[32] = {0};
/* format time as `14 Jul 20:00:08`, and exactly 16 bytes */
strftime(time_buf, sizeof(time_buf), "%d %b %H:%M:%S", localtime(&now));
nb_bytes = snprintf(msg, sizeof(msg), "%s", time_buf);
va_list ap;
va_start(ap, fmt);
vsnprintf(msg + nb_bytes, MAX_LOG_MSG_SZ, fmt, ap);
va_end(ap);
....
}
棘手的事情是当传递长参数(使其长于 32 字节)并将time_buf 更改为小于 32 的其他值(大于 16,例如 31)时,这些代码会抛出堆栈粉碎。经过几分钟的调试,我将vsnprintf的调用行改为
vsnprintf(msg + nb_bytes, MAX_LOG_MSG_SZ - nb_bytes, fmt, ap);
堆栈粉碎消失了,我认为问题已解决。
但是:在time_buf[32](或其他更大尺寸)上,为什么会出现错误调用
vsnprintf(msg + nb_bytes, MAX_LOG_MSG_SZ, fmt, ap);
不扔一个堆栈粉碎?更准确地说,为什么msg 的堆栈粉碎与那个不相关 堆栈 (time_buf) 空间有关?
更新:这是我的uname -a 输出:
Linux coanor 3.5.0-34-generic #55-Ubuntu SMP Thu Jun 6 20:20:19 UTC 2013 i686 i686 i686 GNU/Linux
【问题讨论】:
标签: c debugging stack-overflow