c - fgets() 在多次执行后崩溃

标签 c fgets

我正在编写一个程序来破解 CRC16。我在输出文件并保留计算出的 CRC16 时遇到了一些问题(不知道为什么当我将其写入文件时它会发生变化)。所以我在这里所做的就是读取输入文件,将其写入带有一些乱码的输出文件,然后再次读取输出文件并计算它的 CRC16。如果它与所需的 CRC16 匹配,则完成。然而,经过多次执行后,fgets() 方法因 Seg 错误而崩溃。

有人可以帮助我吗?请忽略性能问题,这是一个测试。

int main(int argc, char* argv[]){

        char outfile[strlen(argv[1])];
        strcpy(outfile,argv[1]);

        strcat(outfile,".crack");

        char crc16[5];
        strcpy(crc16,argv[2]);
        char newcrc16[5];
        char gebrish[80];
        char cat[2];
        int full = 1;
        int p = 0;
        int i,j,k;


        for(i=32; i< 128;i++)
                for(j=32; j< 128; j++)
                        for(k=32; k < 128; k++){
                                gebrish[0] =i;
                                gebrish[1] =j;
                                gebrish[2] =k;
                                gebrish[3] = '\n';
                                gebrish[4] ='\0';

                                boost::crc_16_type result;

                                FILE* file;
                                FILE* out;
                                char line[100];

                                printf("read out\n");
                                out = fopen(outfile,"w");

                                printf("read file\n");
                                file = fopen(argv[1],"r");
                printf("wrt\n");
                                while(fgets(line,80,file) != NULL){
                                        fputs(line,out);
                                }
                                fputs(gebrish,out);

                                fclose(file);
                                fclose(out);

                                printf("read gain\n");
                                out = fopen(outfile,"r");

                                while(fgets(line,80,out) != NULL){
                                        result.process_bytes(line,strlen(line));
                                        printf("%s",line);
                                }

                                int crc = result.checksum();

                                sprintf(newcrc16,"%x",crc);
                                printf("%s",newcrc16);

                                if(strcmp(crc16,newcrc16) == 0){

                                        printf("%s",gebrish);
                                        return 0;

                                }
                        }



        return 0;
}

最佳答案

这会导致缓冲区溢出:

char outfile[strlen(argv[1])];
strcpy(outfile,argv[1]);

strcat(outfile,".crack");

因为outfile中没有足够的空间来终止空字符和“.crack”。它将覆盖不应该的内存,可能是段错误的原因。

更改为:

    char outfile[strlen(argv[1]) + 1 + 6];
    strcpy(outfile,argv[1]);

    strcat(outfile,".crack");

在访问 argv 元素之前,请确保已通过检查 argc 提供它们:

if (argc > 2)
{
    /* Safe to use argv[1] and argv[2]. */
}

还检查 fopen() 的返回值。

关于c - fgets() 在多次执行后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10034912/

相关文章:

c - 在 fork 中写入和读取 - C

php - php中readline与fread/fgets的区别

C fgets() - 只将文件的最后一行写入数组?

32 位和 64 位之间完全不同的输出

c - 具有不同步进源的 Tiva ADC 定序器

c - 需要帮助找出 fgets 的问题是什么

c - 将 fgets 与字符数组一起使用

套接字中的 read() 可以返回 EWOULDBLOCK 吗?

c - 对返回长度为 C 和 C++ 的 char* list[] 的质疑

c++ - GetGuiResources 的使用