0

I got the following error from running this code:

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1074==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x00000042acda (pc 0x000000422519 bp 0x7ffe697712d0 sp 0x7ffe697711f0 T1074)
==1074==The signal is caused by a WRITE memory access.
    #0 0x422518  (/root/sandbox/crack+0x422518)
    #1 0x7fb78fc7ab96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #2 0x402a79  (/root/sandbox/crack+0x402a79)

UndefinedBehaviorSanitizer can not provide additional info.
==1074==ABORTING
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <crypt.h>

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        string hash = argv[1];
        string salt = "AB";
        for (int i = 0; i < 2; i++)
        {
            salt[i] = hash[i]; // The error is here.
        }
        printf("%s\n", salt);
    }
    else
    {
        printf("ERROR\nUsage: ./crack hash\n");
        return 1;
    }
}

Upon doing research I learned that you just cannot assign a character in an array to be a character in another array, but when I tried:

int j = hash[i]; // It worked here.
salt[i] = j; // The error is here.

it still didn't work. Will someone please help me get around this? I need to store the first two characters in the command line argument as a variable of their own.

  • `string salt = "AB";` That points `salt` to a string literal which is not writable. Instead, do `char salt[] = "AB";` – kaylum Dec 17 '19 at 04:12
  • 1
    Does this answer your question? [Why do I get a segmentation fault when writing to a string initialized with "char \*s" but not "char s\[\]"?](https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha) – kaylum Dec 17 '19 at 04:13
  • The short answer is you are trying to *write* to an address that resides in *read-only* memory (on all but a few non-standard compilers) – David C. Rankin Dec 17 '19 at 04:43

2 Answers2

0

You use the CS50 library and header files. In string.h, string is defined as char*. string salt = "AB"; defines a pointer to a literal string. Literal strings are constants and cannot be modified. You need an array of characters instead, e.g.:

char salt[3];

Also, do not forget to paste a 0 at the end of the string after the loop:

salt[i] = 0;

Otherwise, your string will not be terminated. Better yet, call function strncpy instead of writing your own loop:

strncpy(salt, hash, (sizeof salt) - 1);
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Teaching newcomers to the language to use `strncpy()` is probably not doing them a favor. That function [is not very popular since it's hard to use right](https://blog.liw.fi/posts/strncpy/). – unwind Dec 17 '19 at 09:54
  • Thanks! char salt[3]; is what did it for me. I need to study up more on chars and strings, and everything in between! – Jacob LaCorte Dec 21 '19 at 03:03
0

use substr function to get number of character from given position and assign into the another string

char src[] = "substr function Implementation";

int start = 7;
int no_of_char = 12;

char* dest = substr(src, start, no_of_char);

printf("%s\n", dest);
Mahendra Gohil
  • 380
  • 1
  • 3
  • 21