2017-12-31 97 views
2

Ich bin ziemlich neu in C, also habe ich meine Fähigkeiten verfeinert, indem ich an diesem Programm gearbeitet habe, das Passwörter wiederherstellt, indem ich zwei Textdateien nach passenden Hashes durchsuche. Wenn ich versuche, dieses Programm zu kompilieren, erhalte ich den folgenden Fehler.Fehler in Valgrind verstehen

Als solche habe ich versucht, mein Programm mit Valgrind zu debuggen. Da ich neu bei C bin, verstehe ich die Fehler in meinem Programm nicht ganz. Die Fehler sind wie folgt.

Fehler 1: Mögliche Speicherlecks.

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) 

Fehler 2: Ungültige Lese der Größe 1. ich diesen Fehler nicht schwer zu verstehen, da es nicht die anzeigt Linie der Fehler

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 

Mein-Code aufgetreten ist wie folgt:

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

Wenn mir jemand erklären könnte, wo ich falsch gelaufen bin und mir beibringen, wie ich sie beheben kann, wäre ich sehr dankbar.

+3

Code mit 'Kompilieren -g' finden Sie weitere Informationen geben .. Wie Linie Nummer ;). – Stargateur

+0

"Ungültiger Lesewert von Größe 1" => Sieht so aus, als ob Sie ein Zeichen überstrapaziert haben? Nützliche Informationen. – Stargateur

+0

"Adresse 0x0" => Nullzeiger? – Stargateur

Antwort

2

Denken Sie darüber nach, was hier passiert:

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 

wenn es keine $ in der Linie gefunden oder wenn einer der beiden Saiten (oder beides, was das betrifft) leer sind. Die Antwort ist, strstr() wird einen Nullzeiger zurückgeben. Wenn Sie versuchen, das mit einer Formatzeichenfolge %s zu drucken, landen Sie in dem Bereich von undefiniertem Verhalten und einem Segmentierungsfehler.

Ich werde es Ihnen überlassen den Speicherverlust zu finden, die Valgrind erwähnt, aber das ist, warum es Ihnen zu sagen, dass Address 0x0 is not stack'd, malloc'd or (recently) free'd