我在 encode.c:33 中跟踪 coredump。
源代码如下:
if (t->options & TAR_GNU)
strncpy(t->th_buf.magic, "ustar ", 8); // here is the coredump
else
....
函数调用栈是这样的:
0x4064d73a in strncpy (__len=8, __src=0x40663b34 "ustar ", __dest=0x4104e5ed "") at /usr/include/i386-linux-gnu/bits/string3.h:121
0x4024e342 in __strncpy_chk () from /lib/i386-linux-gnu/libc.so.6
0x4024ee1a in __chk_fail () from /lib/i386-linux-gnu/libc.so.6
0x40250065 in __fortify_fail () from /lib/i386-linux-gnu/libc.so.6
0x401b739a in ?? () from /lib/i386-linux-gnu/libc.so.6
0x4017d825 in abort () from /lib/i386-linux-gnu/libc.so.6
0x4017a1df in raise () from /lib/i386-linux-gnu/libc.so.6
0x40064424 in __kernel_vsyscall ()
<signal handler called>
sig_coredump (sig=6) at mpm_common.c:1207
t->th_buf.magic 是 tar_header 的结构,定义:
struct tar_header{
...;
char magic[6];
char version[2];
...;
}
我很确定 strncpy 可以以这种方式使用。
就我而言, t->th_buf 已经有 malloc 了。
在 gdb 中:
(gdb) p t->th_buf
$5 = {name = "/TARFILE.C", '\000' <repeats 89 times>, mode = "100644 ",
uid = " 41", gid = " 41 ", size = " 207114 ", mtime = "12115070475 ",
chksum = "\000\000\000\000\000\000\000", typeflag = 48 '0', linkname = '\000' <repeats 99 times>,
magic = "\000\000\000\000\000", version = "\000",
...