use*_*578 6 c sockets linux gcc
我正在编写一个套接字程序来下载图像.问题是,当我在像gif这样的小图片上测试我的代码时,它运行正常.但是当我使用JPG图片(大于GIF)运行它时,我收到了错误消息:
*** glibc detected *** /home/ubuntu/NetBeansProjects/myDownloader/dist/Debug/GNU-Linux-x86/mydownloader: free(): invalid next size (normal): 0x0a03c978 ***
Run Code Online (Sandbox Code Playgroud)
请查看代码,我将提供有关错误的更多信息.
FILE* pFile;
long lSize;
unsigned char* buffer;
size_t result;
FILE* combinedFile = fopen("mypic.jpg", "wb+");
for(i = 1; i <= numberOfPartitions; i++)
{
sprintf(filename, "part%d", i);
pFile = fopen(filename, "rb");
//obtain file size
fseek(pFile , 0 , SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
buffer = (unsigned char*) malloc(sizeof(unsigned char) * (lSize + 1));
if(buffer == NULL)
{
fputs("Memory error", stderr);
exit(2);
}
// copy the file into the buffer:
result = fread(buffer, 1, lSize, pFile);
if(result != lSize)
{
fputs("Reading error", stderr);
exit(3);
}
else
{
unsigned char* temp = strstr(buffer, "\r\n\r\n");
temp = temp + 4;
int len = lSize - (temp - buffer);
//printf("i : %d len is : %d plen is %f\n",i,len,pLen);
if(i != numberOfPartitions)
fwrite(temp, 1, len - 1, combinedFile);
else
fwrite(temp, 1, len, combinedFile);
}
fclose(pFile);
printf("crash here\n");
free(buffer);
}
fclose(combinedFile);
Run Code Online (Sandbox Code Playgroud)
我从这部分得到了错误,正如我所说,当图像尺寸很小时,它工作正常.但是随着尺寸越来越大,它就破碎了!PS:程序将pic分成几个文件然后重新组合,因此组合部分是导致错误的部分.
任何帮助将非常感谢,因为我已经坚持这个错误超过3天!
Jon*_*ler 12
您不验证fopen()
呼叫是否全部成功; 这是一个麻烦的秘诀.
你不检查它ftell()
给你一个似是而非的价值lSize
.
您不验证strstr()
操作实际上是否找到标记字符串.如果没有,它将返回NULL,然后下面的长度操作就是假的.但是错误表明你的代码已经写出了界限,而不仅仅是读取数据超出范围.
您可以将前四个变量声明到循环体中而不是循环外部.
您不显示变量的声明filename
; 可能是一个没有分配空间的char指针?或者它是一个足够大的数组?
这是一个可能的赌注,有些东西写在一些分配空间的末尾之外.这个代码有什么问题并不是很明显,但问题可能在其他地方,但是这个代码在其他地方遭受了违规的影响.这在记忆问题上很常见; 找到问题的代码不是导致问题的代码.
malloc()
当您分配零字节时,您的机器上是否返回null或非空指针?两者都是合法的回应.
如果ftell()
返回-1,则会malloc()
为0字节分配缓冲区,但是fread()
会尝试读取最多4 GB的数据,这可能会溢出空间.OTOH,如果ftell()
失败,很可能fread()
也会失败.
你打印出文件的大小了吗?它是崩溃的第二个部分文件,还是以后的文件?
我已经使用了您提供的代码,将其作为main()
函数包装,提供了缺少的变量和标题,并在valgrind下运行它.(MacOS X 10.6.6,GCC 4.5.2,Valgrind 3.6.0)它没有问题.所以,你的麻烦很可能不在这个代码本身; 你的程序早期的其他东西被淹没了已分配内存的界限并导致失败.我使用脚本生成了4个部分文件:
{ echo "Header:control-Vcontrol-Mreturncontrol-Vcontrol-M";
dd if=/dev/random bs=1k count=4; } >part1
所以每个文件长4107个字节.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char filename[32];
FILE* pFile;
long lSize;
char *buffer;
ssize_t result;
FILE* combinedFile = fopen("mypic.jpg", "wb+");
int numberOfPartitions = 4;
int i;
for(i = 1; i <= numberOfPartitions; i++)
{
sprintf(filename, "part%d", i);
pFile = fopen(filename, "rb");
fseek(pFile , 0 , SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
printf("size(%d) = %ld\n", i, lSize);
buffer = (char*) malloc(sizeof(char) * (lSize + 1));
if (buffer == NULL)
{
fputs("Memory error", stderr);
exit(2);
}
result = fread(buffer, 1, lSize, pFile);
if (result != lSize)
{
fputs("Reading error", stderr);
exit(3);
}
else
{
char* temp = strstr(buffer, "\r\n\r\n");
temp = temp + 4;
int len = lSize - (temp - buffer);
if(i != numberOfPartitions)
fwrite(temp, 1, len - 1, combinedFile);
else
fwrite(temp, 1, len, combinedFile);
}
fclose(pFile);
printf("crash here\n");
free(buffer);
}
fclose(combinedFile);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果它是我自己的程序,我没有插入所有错误检查.
我的方案中的输出文件长度为16381个字节; 这是3个字节的短.有问题就是fwrite()
电话.该fread()
代码告诉你它有多少字节读取; 你减去了标题的字节数,然后减去一个.所以,这if/else
代码减少到只fwrite()
在else
.
归档时间: |
|
查看次数: |
27517 次 |
最近记录: |