簡體   English   中英

我的代碼需要幫助,它給我一個錯誤控制,可能會到達非void函數的結尾

[英]I need help on my code, it gives me an error control may reach end of non-void function

我被困在cs50的pset2上,說實話,我認為我缺少很多東西,我不知道我是否可以繼續學習而又無法真正理解一些基礎知識。 我試圖做到這一點,然后我想停下來,仍然學習一些有關c的基礎知識。 我需要幫助,以及我將不勝感激的更多評論。

所以基本上這是我的代碼

#define _XOPEN_SOURCE
#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

bool crack(string given_hash);


int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage ./crack hash\n");
        return 1;
    }

    if (!crack(argv[1]))
         return 1;
 }

bool crack(string given_hash)
{    
    string Alphabet = "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char key[6];
    char salt[3];
    salt[2] = '\0';

    for (int x = 0; x < 2; x++)
    {
        salt[x] = given_hash[x];
    }

    // single-letter keys.
    for (int i = 0; i < 52; i++)
    {
        key[0] = Alphabet[i], key[1] = '\0';
        string new_hash = crypt(key,salt);
        if (strcmp(new_hash, given_hash) == 0)
        {
            printf("you got the key: %s\n",key);
            return 0;
        }
    }

    // for 2-letter keys.
    for (int i = 0; i < 52; i++)
    {
        key[0] = Alphabet[i], key[2] = '\0';
        for (int j = 0; j < 52; j++)
        {
            key[1] = Alphabet[j]; 
        }
        string new_hash = crypt(key,salt);
        if (strcmp(new_hash, given_hash) == 0)
        {
            printf("you got the key: %s\n",key);
            return 0;
        }
    }

    // for 3-letter keys.
    for (int i = 0; i < 52; i++)
    {
        key[0] = Alphabet[i], key[3] = '\0';
        for (int j = 0; j < 52; j++)
        {
            key[1] = Alphabet[j];
            for (int k = 0; k < 52; k++)
            {
                key[2] = Alphabet[k];
            }
        }
        string new_hash = crypt(key,salt);
        if (strcmp(new_hash, given_hash) == 0)
        {
            printf("you got the key: %s\n",key);
            return 0;
        }
    }

    // for 4-letter keys.
    for (int i = 0; i < 52; i++)
    {
        key[0] = Alphabet[i], key[4] = '\0';
        for (int j = 0; j < 52; j++)
        {
            key[1] = Alphabet[j];
            for (int k = 0; k < 52; k++)
            {
                key[2] = Alphabet[k];
                for( int l = 0; l < 52; l++)
                {
                    key[3] = Alphabet[l];
                }
            }
        }
        string new_hash = crypt(key,salt);
        if (strcmp(new_hash, given_hash) == 0)
        {
            printf("you got the key: %s\n",key);
            return 0;
        }
    }

    // for 5-letter keys.
    for (int i = 0; i < 52; i++)
    {
        key[0] = Alphabet[i], key[5] = '\0';
        for (int j = 0; j < 52; j++)
        {
            key[1] = Alphabet[j];
            for (int k = 0; k < 52; k++)
            {
                key[2] = Alphabet[k];
                for(int l = 0; l < 52; l++)
                {
                    key[3] = Alphabet[l];
                    for(int m = 0; m < 52; m++)
                {
                    key[4] = Alphabet[m];
                }
            }
        }
    }
    string new_hash = crypt(key,salt);
    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("you got the key: %s\n",key);
        return 0;
    }
  }
}

現在我不知道我在做什么錯,我知道這個錯誤是由於在非void函數中沒有返回的東西,但是我該如何解決呢?

您的main函數和crack函數都有一條代碼路徑,該路徑可能會到達函數的結尾,並且不會返回任何值。

main如果crack返回1true則代碼將到達main的末尾而不返回int值。 但是main有一個例外,如果不顯式返回,則它將return 0 因此,盡管沒有問題,但我仍將確保所有代碼路徑都返回。

crack -如果您的測試均未找到匹配的密碼哈希,則該函數將到達末尾而不會返回。 基於函數邏輯,它永遠不會發生,但是編譯器並不知道。

要解決此問題,您需要確保所有代碼路徑都返回一個值。

您將得到錯誤控制,可能會到達非void函數的末尾,因為crack()函數應該返回boolcrack()函數的末尾沒有return語句。 在編譯代碼時,編譯器發現只有滿足某些條件,才能達到crack()函數的所有返回語句。

bool crack(string given_hash)

{    

    ....
    ....

    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("you got the key: %s\n",key);
        return 0;
    }
    ....
    ....

    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("you got the key: %s\n",key);
        return 0;
    }
    ....
    ....

    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("you got the key: %s\n",key);
        return 0;
    }
    ....
    ....

    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("you got the key: %s\n",key);
        return 0;
    }
    ....
    ....

    if (strcmp(new_hash, given_hash) == 0)
    {
        printf("you got the key: %s\n",key);
        return 0;
    }
}
      //<================ Return statement missing

}

如果不滿足任何條件,則控制可能會達到功能終止。 您應該在crack()函數的末尾添加一個return語句,並返回一個指示失敗的值。 由於在所有情況下您都返回0 (似乎是成功的案例),因此您可以在最后返回1來指示失敗:

    ....
    ....
    return 1;

}

注意, main()函數有一個例外,如果控制到達main()函數的末尾而沒有遇到return語句,則return 0;否則, return 0; 被執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM