简体   繁体   English

我的代码需要帮助,它给我一个错误控制,可能会到达非void函数的结尾

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

I am stuck in pset2 on cs50 , and honestly I think I am missing alot of stuff, I dont know if I can go forward in the course without being able to truely understand some basics. 我被困在cs50的pset2上,说实话,我认为我缺少很多东西,我不知道我是否可以继续学习而又无法真正理解一些基础知识。 I am trying to get this done and then I think I will pause and still learn some basics about c. 我试图做到这一点,然后我想停下来,仍然学习一些有关c的基础知识。 I need help with this as well as more commentary that I would be most grateful for. 我需要帮助,以及我将不胜感激的更多评论。

so basically here's my code 所以基本上这是我的代码

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

now I dont know what I am doing wrong , I know this error is due no no returning thing in a non void function, but how do I fix it ? 现在我不知道我在做什么错,我知道这个错误是由于在非void函数中没有返回的东西,但是我该如何解决呢?

Both your main and crack function have a code path that possibly reach then end of the function and will not return a value. 您的main函数和crack函数都有一条代码路径,该路径可能会到达函数的结尾,并且不会返回任何值。

main - If crack returns 1 or true then the code will reach the end of main without returning an int value. main如果crack返回1true则代码将到达main的末尾而不返回int值。 There is however an exception for main that if you do not explicitly return then it will return 0 . 但是main有一个例外,如果不显式返回,则它将return 0 So while not a problem, I would still ensure that all code paths return. 因此,尽管没有问题,但我仍将确保所有代码路径都返回。

crack - If none of your tests find a matching password hash then the function would reach the end and not return. crack -如果您的测试均未找到匹配的密码哈希,则该函数将到达末尾而不会返回。 Based on the function logic it would never happen, but the compiler does not know that. 基于函数逻辑,它永远不会发生,但是编译器并不知道。

To resolve the issue you need to ensure that all code paths return a value. 要解决此问题,您需要确保所有代码路径都返回一个值。

You are getting error control may reach end of non-void function because the crack() function is suppose to return bool and there is no return statement in crack() function at the end. 您将得到错误控制,可能会到达非void函数的末尾,因为crack()函数应该返回boolcrack()函数的末尾没有return语句。 While compiling the code, the compiler found that all the return statements of crack() function can be reached only if some condition is satisfied 在编译代码时,编译器发现只有满足某些条件,才能达到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

}

and there is a possibility that the control may reach to end of function if none of the conditions is satisfied. 如果不满足任何条件,则控制可能会达到功能终止。 You should add a return statement in the end of crack() function and return a value that indicates failure. 您应该在crack()函数的末尾添加一个return语句,并返回一个指示失败的值。 Since in all cases you are returning 0 (seems that is for success case), may you can return 1 at the end to indicate failure: 由于在所有情况下您都返回0 (似乎是成功的案例),因此您可以在最后返回1来指示失败:

    ....
    ....
    return 1;

}

Note that there is an exception for main() function, if control reaches the end of the main() function without encountering a return statement, return 0; 注意, main()函数有一个例外,如果控制到达main()函数的末尾而没有遇到return语句,则return 0;否则, return 0; is executed. 被执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM