微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检测到*** glibc *** free():下一个大小无效(正常):0x0a03c978 ***

参见英文答案 > Facing an error “*** glibc detected *** free(): invalid next size (fast)”2个
我正在编写一个套接字程序来下载图像.问题是,当我在像gif这样的小图片上测试我的代码时,它运行正常.但是当我使用JPG图片(大于GIF)运行它时,我收到了错误消息:
*** glibc detected *** /home/ubuntu/NetBeansprojects/myDownloader/dist/Debug/GNU-Linux-x86/mydownloader: free(): invalid next size (normal): 0x0a03c978 ***

请查看代码,我将提供有关错误的更多信息.

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,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,len - 1,combinedFile);
        else
            fwrite(temp,combinedFile);
    }

    fclose(pFile);
    printf("crash here\n");
    free(buffer);

}

fclose(combinedFile);

我从这部分得到了错误,正如我所说,当图像尺寸很小时,它工作正常.但是随着尺寸越来越大,它就破碎了!
P.S:程序将pic分成几个文件然后重新组合,因此组合部分是导致错误的部分.

任何帮助将非常感谢,因为我已经坚持这个错误超过3天!

解决方法

您不验证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,i);
        pFile = fopen(filename,"rb");

        fseek(pFile,SEEK_END);
        lSize = ftell(pFile);
        rewind(pFile);
        printf("size(%d) = %ld\n",lSize);

        buffer = (char*) malloc(sizeof(char) * (lSize + 1));

        if (buffer == NULL)
        {
            fputs("Memory error",stderr);
            exit(2);
        }

        result = fread(buffer,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,combinedFile);
            else
                fwrite(temp,combinedFile);
        }

        fclose(pFile);
        printf("crash here\n");
        free(buffer);    
    }

    fclose(combinedFile);
    return 0;
}

如果它是我自己的程序,我没有插入所有错误检查.

我的方案中的输出文件长度为16381个字节;这是3个字节的短.问题是fwrite()调用. fread()代码告诉你它读取了多少字节;你减去了标题的字节数,然后减去一个.所以,如果/ else代码只减少到else中的fwrite().

原文地址:https://www.jb51.cc/c/115869.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐