2017-12-31 137 views
2

私はかなり新しいC言語なので、このプログラムを使ってスキルを磨いています。このプログラムでは、2つのテキストファイルを検索して一致するハッシュを検索します。このプログラムをコンパイルしようとすると、次のエラーが発生します。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の読み込みが無効です" =>あなたが1つの文字を過度に伸ばしているように見えますか?有用な情報。 – Stargateur

+0

"アドレス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 

ラインまたはときに、2つの文字列のいずれかで見つかりませ$(またはその両方がそのことについては、)がないときは空です。答えは、strstr()はヌルポインタを返します。 %s形式の文字列で印刷しようとすると、未定義の動作とセグメンテーションフォールトの領域になります。それはAddress 0x0 is not stack'd, malloc'd or (recently) free'd

関連する問題