2

Program compiles, but I just can't figure out how to go trough each string length generating process without segmentation fault. I could really use some pointers please!

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

int main(int argc, string argv[])
{
if (argc !=2)
{
    printf ("Hash needed!\n");
    return 1;
}
string salt = "50";
string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
string s = "word";
for (int i = 0; i <= 52; i++)
{
    s[0] = alpha[i];
    s[1] = '\0';
    string oneLetter =  crypt(s, salt);
    int check = strcmp(argv[1], oneLetter);
    if (check == 0)
    {
        printf("Password is %s", oneLetter);
        return 0;
    }
    for (int j = 0; j <= 52; j++)
    {
        s[1] = alpha[j];
        s[2] = '\0';
        string twoLetters = crypt(s, salt);
        int check2 = strcmp(argv[1], twoLetters);
        if (check2 == 0)
        {
            printf("Password is %s", twoLetters);
            return 0;
        }
        for (int k = 0; k <= 52; k++)
        {
            s[2] = alpha[k];
            s[3] = '\0';
            string threeLetters = crypt(s, salt);
            int check3 = strcmp(argv[1], threeLetters);
            if (check3 == 0)
            {
                printf("Password is %s", threeLetters);
                return 0;
            }
            for (int l = 0; l <=52; l++)
            {
                s[3] = alpha[l];
                s[4] = '\0';
                string fourLetters = crypt(s, salt);
                int check4 = strcmp(argv[1], fourLetters);
                if (check4 == 0)
                {
                    printf("Password is %s", fourLetters);
                    return 0;
                }
            }
        }
    }
}

}

2 Answers 2

3

string is defined in cs50.h as an alias for char *.

string s = "word"; declares a pointer to char named s and lets it point to the string constant "word".

You could use an array instead, like char s[] = "word";, which is like char s[5] = "word";, creating an array of 5 characters and initialising it to the content of "word", four letters plus a null terminator.

1
  • Lots of thanks for your reply. You were absolutely right. Once I declareds as char it worked fine. Thank you very much. Commented Jun 8, 2017 at 10:42
2

I believe this has something to do with the <= in your for loop. You would iterate 53 instead of 52 times, allowing s[0] = alpha [53] which would be out of range thus causing a segmentation error. I believe that you can apply this to other parts of your code aswell. Instead, < symbol instead of <=. Also, take a look at the other answer given by Blauelf.

1
  • Thank you very much for your answer. I managed to get it working in the end. Yes, there were a few mistakes and that was one of them. Also Blauelf was right. Once I initialized s as char, it worked like a charm. Commented Jun 8, 2017 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .