繁体   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