gpt4 book ai didi

c - C 程序读取文件并转换其字符会产生双重释放或损坏错误

转载 作者:行者123 更新时间:2023-11-30 20:09:55 25 4
gpt4 key购买 nike

执行我的程序时,出现此错误:

*** Error in `./xorcipher': double free or corruption (!prev): 0x00000000024a1010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fe869b317e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8037a)[0x7fe869b3a37a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7fe869b3e53c]
/lib/x86_64-linux-gnu/libc.so.6(fclose+0x103)[0x7fe869b27363]
./xorcipher[0x4009de]
./xorcipher[0x400d68]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fe869ada830]
./xorcipher[0x4007b9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 fd:01 1056628 /home/user1/Dropbox/SCOLARITY/L2Informatique/S3/Projet/Projet xorcipher/Code/programme final/test/xorcipher
00601000-00602000 r--p 00001000 fd:01 1056628 /home/user1/Dropbox/SCOLARITY/L2Informatique/S3/Projet/Projet xorcipher/Code/programme final/test/xorcipher
00602000-00603000 rw-p 00002000 fd:01 1056628 /home/user1/Dropbox/SCOLARITY/L2Informatique/S3/Projet/Projet xorcipher/Code/programme final/test/xorcipher
024a1000-024c2000 rw-p 00000000 00:00 0 [heap]
7fe864000000-7fe864021000 rw-p 00000000 00:00 0
7fe864021000-7fe868000000 ---p 00000000 00:00 0
7fe8698a4000-7fe8698ba000 r-xp 00000000 fd:01 136064 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fe8698ba000-7fe869ab9000 ---p 00016000 fd:01 136064 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fe869ab9000-7fe869aba000 rw-p 00015000 fd:01 136064 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fe869aba000-7fe869c7a000 r-xp 00000000 fd:01 137277 /lib/x86_64-linux-gnu/libc-2.23.so
7fe869c7a000-7fe869e7a000 ---p 001c0000 fd:01 137277 /lib/x86_64-linux-gnu/libc-2.23.so
7fe869e7a000-7fe869e7e000 r--p 001c0000 fd:01 137277 /lib/x86_64-linux-gnu/libc-2.23.so
7fe869e7e000-7fe869e80000 rw-p 001c4000 fd:01 137277 /lib/x86_64-linux-gnu/libc-2.23.so
7fe869e80000-7fe869e84000 rw-p 00000000 00:00 0
7fe869e84000-7fe869eaa000 r-xp 00000000 fd:01 137058 /lib/x86_64-linux-gnu/ld-2.23.so
7fe86a089000-7fe86a08c000 rw-p 00000000 00:00 0
7fe86a0a6000-7fe86a0a9000 rw-p 00000000 00:00 0
7fe86a0a9000-7fe86a0aa000 r--p 00025000 fd:01 137058 /lib/x86_64-linux-gnu/ld-2.23.so
7fe86a0aa000-7fe86a0ab000 rw-p 00026000 fd:01 137058 /lib/x86_64-linux-gnu/ld-2.23.so
7fe86a0ab000-7fe86a0ac000 rw-p 00000000 00:00 0
7ffea9648000-7ffea9669000 rw-p 00000000 00:00 0 [stack]
7ffea96b5000-7ffea96b7000 r--p 00000000 00:00 0 [vvar]
7ffea96b7000-7ffea96b9000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted

问题似乎出在 xor 函数中,但我不知道问题是什么。根据this ,我的问题似乎是由取消引用的指针引起的。但根据 valgrind 的说法,这是一个打开无取消错误(我不知道它是什么,也无法在谷歌上找到任何相关内容)。另一方面,我没有编译错误。

我的代码有什么问题吗?

这是程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void xor(char* name_file_in,char* name_file_out,char* name_key)
{
FILE *file_in;//file to encrypt
FILE *file_out;//encrypted file
unsigned char read_char;//character of the file
int count_char=0;//count number of characters in file
file_in=fopen(name_file_in,"r");
file_out=fopen(name_file_out,"w");

while ( fscanf(file_in,"%c",&read_char) != EOF)//each character of in file is read
{
read_char=read_char^name_key[count_char%strlen(name_key)];//read_char is encrypted according to key's length's modulo
fputc(read_char,file_out);//writing character to out file
count_char++;
}
fclose(file_in);
fclose(file_in);
}

int main(int argc,char* argv[])
{
xor(argv[1],argv[2],argv[3]);
return(0);
}

最佳答案

您将关闭文件两次:

fclose(file_in);
fclose(file_in);

这是未定义的行为。

注意:我只用了 valgrind 5 秒就看到了...在编译调试时使用 -g !

关于c - C 程序读取文件并转换其字符会产生双重释放或损坏错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48027643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com