警告:'fp'在此函数中未初始化使用[-Wuninitialized]

时间:2014-12-21 19:12:57

标签: c

我正在尝试在openFile函数中打开this file并将其传递给函数commonPasswd,但是在我尝试编译时会收到警告。

warning: ‘fp’ is used uninitialized in this function [-Wuninitialized]
     if ( openFile (fp) ) {
          ^

如果我运行它,我会收到Segmentation故障(核心转储)。如需检查,你可以这样做./program 34JBXiZ7tYKF。

#define _XOPEN_SOURCE
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
static const char alphabet[] = "abc";
bool checkPass (const char* key, const char* hash, const char* salt)
{    
    if ( !strcmp (crypt (key, salt), hash) )
        return true;
    return false;
}
bool openFile (FILE* fp)
{
    if ( (fp = fopen ("./commonPasswd.txt", "r")) == NULL ) 
            return false;
    return true;
}
bool commonPasswd (const char* hash, const char* salt)
{
    char* buf = calloc (20, sizeof (char));
    FILE* fp;
    if ( openFile (fp) ) {
        while ( fgets (buf, 20, fp) != NULL ) {
            if ( checkPass (buf, hash, salt) )
                return true;
            free (buf);
        }
    }
    return false;
}
bool DES (char* argv[])
{
    char salt[2];
    strncpy(salt, argv[1], 2 * sizeof (char));
    if ( commonPasswd (argv[1], salt) )
        return true;
    return false;
}
int main (int argc, char* argv[])
{
    if (argc != 2) {
        printf ("Usage: ./crack passwd\n");
        return 1;
    }
    if ( DES (argv) )
        return 0;
    return 2;
}

3 个答案:

答案 0 :(得分:1)

如果要修改指针对象,则需要将指针传递给指针。

bool openFile (FILE **fp)

然后还相应地更新函数调用和函数体。

答案 1 :(得分:1)

指针按值传递。在fp = fopen(...)内完成作业openFile时,这不会更改fpcommonPasswd的值。

您需要将指针指针传递给openFile,更改openFile以返回FILE*,或者只是删除该功能并直接调用open

答案 2 :(得分:0)

bool commonPasswd (const char* hash, const char* salt)
{
    char* buf = calloc (20, sizeof (char));
    FILE* fp;
    printf ("Checking if the password is a common one\n");
    if ( openFile (&fp) ) {
        while ( fgets (buf, 20, fp) != NULL ) {
            if ( checkPass (buf, hash, salt) )
                return true;
            free (buf);
        }
    }
    printf ("The password isn't a common one\n");
    return false;
}
bool openFile (FILE** fp)
{
    if ( (*fp = fopen ("./commonPasswd.txt", "r")) == NULL ) {
        system ("wget -O commonPasswd.zip 'http://xato.net/files/10k%20most%20common.zip'");
        system ("unzip commonPasswd.zip");
        remove ("commonPasswd.zip");
        rename ("10k most common.txt", "commonPasswd.txt");
        if ( (*fp = fopen ("./commonPasswd.txt", "r")) == NULL ){
            printf ("The file couldn't be openend\n");
            return false;
        }
    }
    return true;
}