0

When I run my code:

#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>

string crack(string hashed);
string deleteAll(string s, char target);

// Deals with input and stuff and feeds info into functions.
int main(int argc, string argv[])
{
    // Returns 1 because this is an error (because there is no hashed pw).
    if (argc != 2)
    {
        printf("Usage: ./crack hash\n");
        return 1;
    }
    else
    {
        // Makes crack do all the 'hard work' of guess/checking.
        string pw = crack(argv[1]);
        // Prints the pw.
        printf("%s\n", pw);
        return 0;
    }
}

// Does all the 'hard work'.
string crack(string hashed)
{
    // A string is a list of chars, so this is easier than doing characters[0] = '\0'; ...
    string characters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
           // A temporary variable that is useful for guessing.
           temp = malloc(5),
           // The salt for crypt() (salt will be defined in next two lines).
           salt = malloc(3),
           // The output.
           out = malloc(5);
    salt[0] = hashed[0];
    salt[1] = hashed[1];
    salt[2] = '\0';
    // Length will be used in the for loops in the next lines.
    int length = (int) strlen(characters),
        c1 = 0,    // c1-4 are counters for the while loops.
        c2 = 0,
        c3 = 0,
        c4 = 0;
    // These things check if each loop is done.
    bool done1 = false,
         done2 = false,
         done3 = false,
         done4 = false;
    while (!done1)
    {
        while (!done2)
        {
            while (!done3)
            {
                while (!done4)
                {
                    // Sets temp to the current pw (the loops guess in the following order: a, b, c, d, e..., Z, aa, ab, ac...zz, Aa, ...).
                    temp[0] = characters[c1];
                    temp[1] = characters[c2];
                    temp[2] = characters[c3];
                    temp[3] = characters[c4];
                    temp[4] = '\0';
                    temp = deleteAll(temp, ' ');
                    // Checks if the pw is the correct one.
                    if (strcmp(crypt(temp, salt), hashed) == 0)
                    {
                        out = temp;    // Sets out to temp, and exits the loops
                        done1 = true;
                        done2 = true;
                        done3 = true;
                        done4 = true;
                    }
                    if ((c1 == length - 1) &  // Sees if it should stop.
                        (c2 == length - 1) &
                        (c3 == length - 1) &
                        (c4 == length - 1))
                    {
                        out = "Unknown hash";    // Sets out to temp, and exits the loops
                        done1 = true;
                        done2 = true;
                        done3 = true;
                        done4 = true;
                    }
                    c4++;
                }
                c3++;
            }
            c2++;
        }
        c1++;
    }
    return out;
}

string deleteAll(string s, char target)
{
    // Gets a string length.
    int length = (int) strlen(s), count = 0;
    // Sets the length of the output to the length of the input string (which is faster than calculating).
    string out = malloc(length);
    // Scans the input string and checks for target.
    for (int i = 0; i < length; i++)
    {
        if (!(s[i] == target))
        {
            out[count] = s[i];
            count++;
        }
    }
    return out;
}

It always gives me this error:

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1368==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000439000 (pc 0x00000042
89c7 bp 0x7ffc6391da50 sp 0x7ffc6391d750 T1368)
==1368==The signal is caused by a READ memory access.
    #0 0x4289c6 in crack /root/sandbox/crack.c:65:31
    #1 0x42809e in main /root/sandbox/crack.c:21:21
    #2 0x7f346c551b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.
c:310
    #3 0x402b89 in _start (/root/sandbox/crack+0x402b89)

UndefinedBehaviorSanitizer can not provide additional info.
==1368==ABORTING

Can someone tell me (or at least help me with) what this error means?

1

The UndefinedBehaviorSanitizer (aka UBSan) relates to execution-time bugs caused by various kinds of undefined programme behaviour. For instance,

  • Using misaligned or null pointer,
  • Signed integer overflow, or
  • Conversion to, from, or between floating-point types which would overflow the destination.

The odds are the line 65 seating characters[c4] access an invalid index.

You must log in to answer this question.

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