我有一个相当简单的程序,我用 snprintf 写入一个字符串,如果需要,我会扩展字符串长度以适应我要写入的内容。然后,我尝试添加到该字符串。它确实有效,但是当我释放 malloc 的变量时,它崩溃了。
编码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int ac, char **av) {
char buf[16];
char *str = buf;
char *extra = NULL;
int len;
if(!av[1]) {
return 0;
}
if((len = snprintf(str, 16, "%s", av[1])) >= 16) {
printf("more than 15 chars, %d\n",len);
if (extra = malloc((len + 1) * sizeof(char))) {
snprintf(str = extra, len + 1, "%s", av[1]);
}
} else {
printf("less than 15 chars, %d\n",len);
}
printf("%s\n", str);
if((len = snprintf(str+strlen(str), 16, "%s", av[1])) >= 16) {
printf("more than 15 chars, %d, required: %d\n",len,(len + strlen(av[1]) + 1));
if ((extra = malloc((len + strlen(av[1]) + 1) * sizeof(char)) ) && strcpy(extra,str)) {
str = extra;
snprintf(extra+strlen(av[1]), len + 1, "%s", av[1]);
}
} else {
printf("less than 15 chars, %d\n",len);
}
printf("%s\n", str);
if(extra) {
free(extra);
}
return 1;
}
输出:
more than 15 chars, 16
1234567890123456
more than 15 chars, 16, required: 33
12345678901234561234567890123456
*** glibc detected *** ./testprog: free(): invalid next size (fast): 0x0000000000ce0030 ***
======= Backtrace: =========
/lib/libc.so.6(+0x78a56)[0x7fe46c0b9a56]
./testprog[0x4008c7]
/lib/libc.so.6(__libc_start_main+0xf5)[0x7fe46c062455]
./testprog[0x4005a9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:03 26355255 /home/joachim/kc/testprog
00600000-00601000 rw-p 00000000 08:03 26355255 /home/joachim/kc/testprog
00ce0000-00d01000 rw-p 00000000 00:00 0 [heap]
7fe46be2c000-7fe46be41000 r-xp 00000000 08:03 15077980 /usr/lib/libgcc_s.so.1
7fe46be41000-7fe46c040000 ---p 00015000 08:03 15077980 /usr/lib/libgcc_s.so.1
7fe46c040000-7fe46c041000 rw-p 00014000 08:03 15077980 /usr/lib/libgcc_s.so.1
7fe46c041000-7fe46c1d8000 r-xp 00000000 08:03 393241 /lib/libc-2.15.so
7fe46c1d8000-7fe46c3d8000 ---p 00197000 08:03 393241 /lib/libc-2.15.so
7fe46c3d8000-7fe46c3dc000 r--p 00197000 08:03 393241 /lib/libc-2.15.so
7fe46c3dc000-7fe46c3de000 rw-p 0019b000 08:03 393241 /lib/libc-2.15.so
7fe46c3de000-7fe46c3e2000 rw-p 00000000 00:00 0
7fe46c3e2000-7fe46c403000 r-xp 00000000 08:03 393253 /lib/ld-2.15.so
7fe46c5d5000-7fe46c5d8000 rw-p 00000000 00:00 0
7fe46c600000-7fe46c603000 rw-p 00000000 00:00 0
7fe46c603000-7fe46c604000 r--p 00021000 08:03 393253 /lib/ld-2.15.so
7fe46c604000-7fe46c605000 rw-p 00022000 08:03 393253 /lib/ld-2.15.so
7fe46c605000-7fe46c606000 rw-p 00000000 00:00 0
7fff72b84000-7fff72ba5000 rw-p 00000000 00:00 0 [stack]
7fff72bff000-7fff72c00000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
现在我不完全是你所说的 C 专家,所以这个错误可能相当简单,但我自己无法解决这个问题/在网上找到任何有用的东西。
谢谢您的帮助!
编辑:
固定代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int ac, char **av) {
char buf[16];
char *str = buf;
char *extra = NULL;
int len;
int ssize = 16;
if(ac != 2)
{
return 0;
}
if((len = snprintf(str, 16, "%s", av[1])) >= 16) {
printf("more than 15 chars, %d\n",len);
if (extra = malloc((len + 1) * sizeof(char))) {
ssize = (len + 1);
snprintf(str = extra, len + 1, "%s", av[1]);
}
} else {
printf("less than 15 chars, %d\n",len);
}
printf("%s\n", str);
char *extra2 = NULL;
printf("space: %d (%d-%d)\n",ssize-strlen(str),ssize,strlen(str));
if((len = snprintf(str+strlen(str), ssize-strlen(str), "%s", av[1])) >= ssize-strlen(str)) {
printf("more than 15 chars, %d, required: %d (strlen: %d)\n",len,(len + strlen(av[1]) + 1),strlen(str));
if ((extra2 = malloc((len + strlen(av[1]) + 1) * sizeof(char)) ) && strcpy(extra2,str)) {
str = extra2;
ssize = (len + strlen(av[1]) + 1);
snprintf(extra2+strlen(av[1]), len + 1, "%s", av[1]);
} else printf("failed extraing");
} else {
printf("less than 15 chars, %d\n",len);
}
printf("%s\n", str);
if(extra) {
free(extra);
}
if(extra2) {
free(extra2);
}
return 1;
}