【发布时间】:2019-09-01 20:14:31
【问题描述】:
我正在学习 c 编程。下面的程序向我展示了输出,但是当它执行 free 方法时。这是给我错误:- free(): invalid next size (normal) 。请让我知道我缺少什么。
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE *fp;
char r[1024];
fp = popen("/bin/ls /etc/", "r");
if (fp == NULL) {
perror("Failed to run the command");
exit(-1);
}
int totallengthread = 0, alloc_size = 1024;
char *buffer = (char*) calloc(alloc_size , sizeof(char));
int lenofbuff = 0;
while((lenofbuff=fread(r,sizeof(char),1024,fp))>0){
totallengthread += lenofbuff;
if (totallengthread >= alloc_size) {
alloc_size += 1024;
buffer = realloc(buffer, alloc_size);
}
concat(buffer, r);
}
printf("this is the output =>%s", buffer);
pclose(fp);
free(buffer);
return 0;
}
void concat(char *dest, const char *source) {
char *d = dest;
char *s = source;
while (*d != '\0') {
d++;
}
while (*s != '\0') {
*d++ = *s++;
}
*d = '\0';
}
【问题讨论】:
-
您最多读取 1024 个字节,然后将其连接到缓冲区。这很容易超过缓冲区的大小。例如,如果缓冲区中当前有 700 个字节,并且您连接 800 个字节,那么您将在缓冲区中放入 1500 个字节。这会溢出缓冲区并破坏内存中的其他数据。在将数据放入其中之前,请确保缓冲区足够大以容纳数据,而不是之后。另外,请注意您的计数是否包括终止空字符。如果您将 1024 个非空字符和一个终止空字符放入一个 1024 字节的缓冲区,那么您已经超出了它。
-
埃里克·波斯托伊希尔。我正在检查大小并使用 realloc 扩展大小。它随机抛出此错误。
-
您更改了问题中的代码。现在这是一个不同的问题。
-
@Vipin 你试过在 valgrind 和/或 address sanitizer 下运行它吗?
-
Eric Postpischil 是的,你是对的,我更新了代码。即使在这两种情况下,它都会引发相同的错误。这是我粘贴代码时的错误。