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

问题描述 投票:0回答:2

我在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函数中没有返回的东西,但我该如何修复呢?

c cs50
2个回答
1
投票

你得到错误控制可能会达到非空函数的结束,因为crack()函数假设返回bool并且最后在crack()函数中没有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;


1
投票

你的maincrack函数都有一个代码路径,可能会到达函数的末尾并且不会返回值。

main - 如果破解返回1true,那么代码将到达main的末尾而不返回int值。然而,main有一个例外,如果你没有明确地返回那么它将return 0。所以虽然不是问题,但我仍然会确保所有代码路径都返回。

crack - 如果您的测试都没有找到匹配的密码哈希值,那么该函数将到达终点而不返回。基于函数逻辑,它永远不会发生,但编译器不知道这一点。

要解决此问题,您需要确保所有代码路径都返回一个值。

© www.soinside.com 2019 - 2024. All rights reserved.