Anti_Virus.exe引发了断点。 C语言 [英] Anti_Virus.exe has triggered a breakpoint. C language

查看:133
本文介绍了Anti_Virus.exe引发了断点。 C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个反病毒(不是一个真实的 - 只是为了学习),它使用堆,有时当它需要使用它与关于这个专题的消息打破了堆,然后它给我的留言:
调试断言失败
和:
恩pression:_crtisvalidheappointer(puserdata)
我真的不知道为什么?
在code它在功能运行时发生while循环是他在这一行第五迭代:

I wrote an "Anti Virus" (not a real one - just to learn) which uses the heap and sometimes when it needs to use the heap it's break with the message on the topic then it gives me the message: "Debug Assertion Failed" and: "expression: _crtisvalidheappointer(puserdata)" and i realy don't know why... In the code it's happen in the function run when the while loop is on his 5th iteration in this line:

if (!(results = (char**)realloc(results, sizeof(results) + sizeof(char*))))

功能:

void run(char* dir_path, char* virus_path, char mode)
{
    DIR* dir = NULL;
    FILE* virus = NULL;
    struct dirent* cur_file;  // cur_file is a pointer for struct dirent which represnts the file we are checking now (current file)
    char** results = NULL;  // resullts will be an array of strings to write in the log
    int results_len = 0, i = 0;
    char* file_path = NULL;

    //checks the arguments:
    if (!(dir = opendir(dir_path)))  // argv[1] should be the directory
    {
        printf("The path that given as the first argument doesn't point to a directory / ");
        printf("an error has occurred while opening the directory\n");
        return -1;
    }
    if (!(virus = fopen(virus_path, "rb")))
    {
        printf("The path that given as the second argument doesn't point to a file / ");
        printf("an error has occurred while opening the file\n");
        closedir(dir);
        return -1;
    }

    //running on the file in the directory:
    while (cur_file = readdir(dir))  // at the end of the directory readdir() will return NULL
    {
        if (!(strcmp(cur_file->d_name, ".")))  // at the first time wer'e reading from a directory the value of d_name will be "."
        {
            continue;
        }
        if (!(strcmp(cur_file->d_name, ".."))) //at the second time wer'e reading from a directory the value of d_name will be ".."
        {
            continue;
        }

        if (!(file_path = (char*)malloc(strlen(dir_path) + cur_file->d_namlen + 2))) //1 for \ between dir_path and d_name and 1 for the NULL
        {
            closedir(dir);
            fclose(virus);
            return -1;
        }
        strcpy(file_path, dir_path);
        strcat(file_path, "\\");
        strcat(file_path, cur_file->d_name);
        if (!(results)) // if results == NULL -> if didn't allocated memory for results already
        {
            if (!(results = (char**)malloc(sizeof(char*))))
            {
                printf("Problem with malloc\n");
                free(file_path);
                closedir(dir);
                fclose(virus);
                return -1;
            }
        }
        else
        {
            if (!(results = (char**)realloc(results, sizeof(results) + sizeof(char*))))
            {
                printf("Problem with realloc\n");
                for (i = 0; i < results_len; i++)
                {
                    free(results[i]);
                }
                free(file_path);
                free(results);
                closedir(dir);
                fclose(virus);
                return -1;
            }
        }    
        results[results_len] = check_file(file_path, virus, mode);
        if(results[results_len] == -1) // results_len will be updated later (just malloced)
        {
            for (i = 0; i < results_len; i++)
            {
                free(results[i]);
            }
            free(file_path);
            free(results);
            closedir(dir);
            fclose(virus);
            return -1;
        }
        results_len++;
        free(file_path);
    }
    fclose(virus);
    closedir(dir);
    write_to_log(dir_path, virus_path, mode, results, results_len);
}

和功能check_file返回一个malloced在check_file,将在其他功能免费一个char *(字符串)。

and the function check_file returns a char* (string) which malloced in check_file and will be free in other function.

请问有人知道原因吗?谢谢

Does someone knows the reason? Thank You

推荐答案

这行:

        if (!(results = (char**)realloc(results, sizeof(results) + sizeof(char*))))

时不增加的结果(因此你是踩在它的结束为 result_len 的大小增加)。你可能想使用(result_len + 1)* sizeof的(字符*),因为你已经存储在结果字符串的数量

Is not increasing the size of results (and therefore you are stomping over the end of it as result_len increases). You probably want to use (result_len + 1)*sizeof(char*), since you are already storing the number of strings in results.

这篇关于Anti_Virus.exe引发了断点。 C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆