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.