gpt4 book ai didi

c - 释放 malloc 的内存失败

转载 作者:行者123 更新时间:2023-12-02 05:38:24 26 4
gpt4 key购买 nike

我有一个相当简单的程序,我使用 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;
}

最佳答案

在对字符串的第二次复制中,您使用了这段代码,它告诉 snprintf() 缓冲区中有 16 个字节:

if((len = snprintf(str+strlen(str), 16, "%s", av[1])) >= 16) {

但是您已经将 str + strlen(str) 作为要写入的地址。显然,您没有从该地址获得 16 个可用字节,您只有 1 个(假设您最初输入的字符串长度超过 16,然后您分配了 len + 1 个字节)。因此,当您告诉 snprintf() 有 16 个时,它会写入超过您分配的内存的末尾,这有时只会在您尝试释放它时显示为问题。

关于c - 释放 malloc 的内存失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11352311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com