2017-12-31 60 views
2

저는 C에 상당히 익숙합니다. 따라서 해시에 맞는 두 개의 텍스트 파일을 검색하여 암호를 복구하는이 프로그램을 사용하여 실력을 연마했습니다. 이 프로그램을 컴파일하려고하면 다음 오류가 발생합니다.Valgrind의 오류 이해

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; 
} 

누군가 내가 잘못 생각한 곳을 설명하고 문제를 해결하는 방법을 안내하면 매우 감사하게 생각합니다.

+3

가'로 코드를 컴파일 당신을 말하는 이유

라인처럼 .. -g' 당신에게 더 많은 정보를 제공합니다 나는 Valgrind의 언급 메모리 누수를 찾기 위해 당신을 떠날거야, 그러나 이것은이다 번호;). – Stargateur

+0

"크기 1의 유효하지 않은 읽기"=> 마치 한 문자를 지나치게 많이 읽은 것처럼 보입니까? 유용한 정보. – Stargateur

+0

"Address 0x0"=> NULL 포인터입니까? – Stargateur

답변

2

여기서 일어나는 일에 대해 생각해

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()은 null 포인터를 반환합니다. %s 형식 문자열로 인쇄하려고하면 정의되지 않은 동작과 세분화 오류가 발생합니다. 이 Address 0x0 is not stack'd, malloc'd or (recently) free'd