了解 Valgrind 中的错误
Understanding errors in Valgrind
我对 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 arguments 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 arguments and
//End program if user does not enter exactly 3 arguments
if(argc < 3 || argc > 3) {
UsageInfo(argv[1],argv[2]);
exit(1);
}
system("cls");
//Pass command line arguments 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()
将 return 一个空指针。当您尝试使用 %s
格式字符串打印它时,您最终会陷入未定义行为和分段错误的境界。
我会留给您查找 Valgrind 提到的内存泄漏,但这就是它告诉您 Address 0x0 is not stack'd, malloc'd or (recently) free'd
的原因
我对 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 arguments 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 arguments and
//End program if user does not enter exactly 3 arguments
if(argc < 3 || argc > 3) {
UsageInfo(argv[1],argv[2]);
exit(1);
}
system("cls");
//Pass command line arguments 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()
将 return 一个空指针。当您尝试使用 %s
格式字符串打印它时,您最终会陷入未定义行为和分段错误的境界。
我会留给您查找 Valgrind 提到的内存泄漏,但这就是它告诉您 Address 0x0 is not stack'd, malloc'd or (recently) free'd