I'm certain that the error is from fgets. The dictionary I'm using for this pset is preprocessed(?) so that all the words are ≤ 8.

I had a segmentation fault earlier with copying the first 2 characters from the user inputted password. After searching, I found I could solve it by using memset to fill all 3 chars with terminating characters.

I tried the same thing with this seg fault, but to no avail. I also tried changing the file pointer to one in which I have only one line, 7 characters in length→ this didn't work either.

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

int main (int argc, char* argv[])
    FILE* crackDict = fopen("/Dropbox/CS50/hacker2/CrackDictionary/Cracked_Dictionary.txt", "r");   
    string encryptedPass, crackWord, salt = NULL, engWord = NULL;

    if ((argc == 2) && (strlen(argv[1]) == 13))
        encryptedPass = argv[1];
        printf("Usage: ./Crack [encrypted password...13 chars long]\n");
        return 0;

    //clear the salt with terminating chars and copy first 2 chars of userinput
    memset(salt, '\0', 3);  
    strncpy(salt, encryptedPass, 2);

    memset(engWord, '\0', 9);
    for (int i = 0; fgets (engWord, 9, crackDict)!= NULL; i++)
        crackWord = crypt(engWord, salt);
        if (strcmp(crackWord, encryptedPass) == 0)
            memset(engWord, '\0', 9);   
    printf("%s\n", engWord);

    return 0;    

Any help is greatly appreciated.

1 Answer 1


Not really. The cause of the error involves some topics that you will study later in the course. I'll try to simplify my answer as much as possible, but don't panic if you don't fully understand what I say because this is often a confusing topic even for students who already got to this stage in the course.

  • C does not have a type named string. The cs50 library defines the type synonym string for another built-in type, namely char *.

  • char * is what is referred to as a pointer type. A variable of a pointer type is used to store (point to) a memory address (i.e., an address of a location in memory).

  • The value NULL represents a memory address here (address 0 or the 0th location in memory). The 0th location in memory is a special location that you can't read/write data from/to it.

Initially, you set salt and engWord to NULL, then applied them to memset which tries to write data into the locations that they point to and that is what's causing the error (because you can't read/write data from/to the 0th location in memory).

A quick fix to that would be declaring your string variables as char [] rather than string.

#define LENGTH 3
char salt[LENGTH];
memset(salt, '\0', LENGTH * sizeof (char)); // should work just fine
  • Yes, I know what pointers are from watching a video series before :). They make sense to me now :D By the way, I believe you may have accidentally swapped the first two parameters for memset. And in a previous version of this code I did exactly what you did above. I don't think the issue is from what you said, considering I had all my strings declared as character arrays at one point. I did try what you did though. I'm still left with the segmentation fault. When I ran the code through gdb, it seg fault only occurred at the line of fgets
    – Jon
    Dec 29, 2014 at 5:42
  • GDB screenshot: puu.sh/dNsUl/6dfbcaa44e.png→ It's just not grabbing the word from my dictionary.
    – Jon
    Dec 29, 2014 at 5:55
  • @TomJacob thanks for the heads-up! Edited! Did you check whether crackDict is NULL? It appears from the screenshot that it is NULL already!
    – kzidane
    Dec 29, 2014 at 5:58
  • Forgot about that! I'll try it now :)
    – Jon
    Dec 29, 2014 at 6:01
  • It doesn't return NULL when I enter a valid 2nd argument (13 chars), but it does return null when it's less than 13 characters. Interesting. Do you know why this could be? The implementation: puu.sh/dNtCj/f1a8ca08cd.png
    – Jon
    Dec 29, 2014 at 6:05

You must log in to answer this question.

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