Home > Enterprise >  Why does my program return "Segmentation fault (core dumped)" when I remove a conditional
Why does my program return "Segmentation fault (core dumped)" when I remove a conditional

Time:10-17

I've writen a program that allows me take a command argument of 26 alpha chars to be used as a key at a latter stage of my program to encipher text.

To do this, I had to (a) verify user input alpha chars (b) chars are 26 and (c) none of them repeat.

I decided to put this all in one function(as you'll see below). This has been succesful.

How ever, when I was trying to check whether the key is indeed 26 chars (in my main function) using an else if condition else if (argc != 2 || (is_valid_key(argv[1]) != 1 || is_valid_key(argv[2]) == 2)) I thought that (is_valid_key(argv[1]) != 1 may be useless here sinced I used it in if.

When I did this(regardless of my command argument) my program returned Segmentation fault (core dumped) and I am trying to understand why.

The line causing the problem is else if in my main function.

for context:

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

int is_valid_key(string verify);
int main(int argc, string argv[])
{
    //check whether the user input a valid comand line arguement
    if (argc != 2 || is_valid_key(argv[1]) == 1)
    {
        printf("Usage: ./substitution key\n"); //re-prompt user if they didn't enter a digit
        return 1;
    }
    else if (argc != 2 || is_valid_key(argv[2]) == 2))
    {
        printf("Key must only contain alphabetic characters.\n");
        return 1;
    }

    string plain_text = get_string("plaintext: ");

    printf("%s\n", plain_text);

}



int is_valid_key(string verify)
{
    int i;
    for (i = 0; i < verify[i]; i  )
    {
    char ch = verify[i];
   if (!isalpha(ch))
        {
            return 1; //return for alpha
        }
     }

    if (i != 26)
    {
        return 2; //return for 26 chars
    }


    for (int a = 0; verify[a] != '\0'; a  )
    {
        for (int b = 0; b < a; b  )
        {
            // if 2 characters match, the key is not valid
            if (verify[a] == verify[b])
                return 3; // return for no repeate
        }

    }

    return 4; //return all correct
}

CodePudding user response:

The code is not checking the correct argument in main().

It's testing is_valid_key(argv[2]) but in your test case of only a single command-line parameter:

# ./main qwertyuiopasdfghjklzxcvbnm

There is only:

  • argv[0] which points to the string "./main" (or whatever your executable is called)
  • argv[1] which points to the string "qwertyuiopasdfghjklzxcvbnm"

The referenced argv[2] does not exist, and references "off the end" of argv, hence the segfault.

Changing this line to:

else if (argc >= 2 || is_valid_key(argv[1]) == 2)   // <<-- HERE
{
    printf("Key must only contain alphabetic characters.\n");
    return 1;
}

will fix it.

CodePudding user response:

Segmentation fault means that you are trying to access a memory location which you shouldn't have. The indexing of arguments starts from 0. And there should be only 2 command-line arguments in this program. So, you are trying to access the index 2 which is out of bounds currently. So, your program exits saying 'Segmentation Fault'.

  • Related