2014-12-21 403 views
-1

我試圖在函數openFile中打開this file並將其傳遞給函數commonPasswd,但在嘗試編譯時收到警告。警告:'fp'在此函數中未初始化使用[-Wuninitialized]

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

如果我運行它,我得到一個分段錯誤(核心轉儲)。檢查你可以做./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; 
} 
+0

'crypt'函數是否知道'salt'完全是2個字符長?因爲'salt'不一定包含'\ 0'值,所以如果它在'crypt'內部被處理爲一個字符串,這可能導致seg錯誤。 –

+0

是的,它知道鹽的長度是2個字符,或者它也可以接受像'$ 1 $ anySizeSALT $'這樣的參數用於md5加密。 – Arturo

回答

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

如果要修改指針對象,則需要將指針傳遞給指針。

bool openFile (FILE **fp) 

然後也相應地更新函數調用和函數體。

+0

當我更新函數調用和函數體時,出現很多錯誤。我能看到什麼來理解這一點? – Arturo

+0

@Arturo編輯你的問題,並在問題末尾添加新程序的附錄 – ouah

+0

好的,我編輯了它。 – Arturo

1

指針按值傳遞。當fp = fopen(...)分配在openFile內完成時,這不會更改中的fp的值。

您需要將指針指向openFile,將openFile更改爲返回FILE*,或者直接刪除該函數並直接調用open

相關問題