我是C的新手,所以我一直在磨练我的技能,通过搜索两个文本文件来匹配哈希来恢复密码。当我尝试编译此程序时,我收到以下错误。
Segmentation fault (core dumped)
因此,我尝试使用valgrind调试我的程序。因为我对C很新,所以我不太了解程序中的错误。错误如下。
错误1:可能的内存泄漏。
552 bytes in 1 blocks are still reachable in loss record 1 of 2
at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-
linux.so)
by 0x4EA7CDC: __fopen_internal (iofopen.c:69)
by 0x4008E7: matchfile (in /home/st2411/test)
by 0x400873: main (in /home/st2411/test)
错误2:读取大小为1无效。我发现此错误很难理解,因为它没有指出错误发生在哪一行
Invalid read of size 1
at 0x4EE4070: __strstr_sse2_unaligned (strstr-sse2-unaligned.S:22)
by 0x4009DA: matchfile (in /home/st2411/test)
by 0x400873: main (in /home/st2411/test)
Address 0x0 is not stack'd, malloc'd or (recently) free'd
我的守则如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXCHAR 1000
//Declaring Functions to match word in file
int matchfile(char *shadowfilename, char
*hashtablefilename);//shadowfilename for shadow.txt hashtablefilename for hash table
void UsageInfo(char *shadowfile, char * hashtablefile );//Display usage info on arguements for program
void UsageInfo(char *shadowfile, char * hashtablefile) {
printf("Usage: %s %s <shadowfile> <hashtable>\n", shadowfile, hashtablefile);
}
//main function.
int main(int argc, char *argv[]) {
int result;
int errcode;
//Display format for user to enter arguements and
//End program if user does not enter exactly 3 arguements
if(argc < 3 || argc > 3) {
UsageInfo(argv[1],argv[2]);
exit(1);
}
system("cls");
//Pass command line arguements into searchstringinfile
result = matchfile(argv[1], argv[2]);
//Display error message
if(result == -1) {
perror("Error");
printf("Error number = %d\n", errcode);
exit(1);
}
return(0);
}
//Declaring Functions to match word in file
int matchfile(char *shadowfilename, char *hashtablefilename){
//Declare file containing user account and hashed password
FILE *shadowfile;
//Declare file containing list of words and corresponding hash values
FILE *hashtable;
//char variables to extract text from files
char strshadow[MAXCHAR];
char strhash[MAXCHAR];
//read from file containing user account and hashed password
shadowfile = fopen(shadowfilename, "r");
//error message if file does not exist
if (shadowfile == NULL){
printf("Could not open file %s",shadowfilename);
return 1;
}
//read from file containing list of words and corresponding hash values
hashtable = fopen(hashtablefilename, "r");
//error message if file does not exist
if (hashtable == NULL){
printf("Could not open file %s",hashtablefilename);
return 1;
}
const char ch = '$';
//char variables to extract hash values
//char *strshadowvalues for shadow file;
//char *strhashvalues for hash table file;
//Valgrind detected an error here
char *strshadowvalues;
char *strhashvalues;
//variable to check line number for matched
int linenumber = 1;
//Variable to count match results
int search_result = 0;
while (fgets(strshadow, MAXCHAR, shadowfile) != NULL && fgets(strhash, MAXCHAR, hashtable) != NULL){
strshadowvalues = strchr(strshadow, ch);
strhashvalues = strchr(strhash, ch);
//Matching hashes line-by-line
if((strstr(strshadowvalues,strhashvalues)) != NULL) {
//Display lines in which matched hash is found
printf("A match found on line: %d\n", linenumber);
//Display matching hash in shadow file
printf("Shadow:\n%s\n", strshadow);
//Display matching hash in shadow file
printf("Hash: \n%s\n", strhash);
search_result++;
}//Display message if no match
if((strstr(strshadowvalues,strhashvalues)) == NULL|| strshadowvalues==NULL || strhashvalues==NULL) {
printf("No password found ");
}
linenumber++;
}
//close file
fclose(shadowfile);
return 0;
}
如果有人能向我解释我出错的地方并指导我如何解决这些问题,我将非常感激。
想想这里发生的事情:
strshadowvalues = strchr(strshadow, ch);
strhashvalues = strchr(strhash, ch);
//Matching hashes line-by-line
if((strstr(strshadowvalues,strhashvalues)) != NULL) {
//Display lines in which matched hash is found
如果在行中找不到$
,或者当两个字符串中的一个(或两者都是空的)为空时。答案是,strstr()
将返回一个空指针。当您尝试使用%s
格式字符串打印时,最终会出现未定义行为和分段错误。
我会留给你找到Valgrind提到的内存泄漏,但这就是为什么它告诉你Address 0x0 is not stack'd, malloc'd or (recently) free'd