ccs50caesar-cipher

CS50 Week 2 Caesar


this is just a preliminary part of code that i tried running for checking the condition for the command line argument having an input which was numeric with digits ranging from 0-9. if empty, it forces the user for an input. the program is successfully printing the error message for when there is non numeric input in command line, or if there is more than one input in command line. but whenever i execute with no input at command line, it shows "Segmentation fault (core dumped)" even after i have added my own error message in my code for the situation if there is no input at command line. my problem is the fact that it won't show my error message instead

the code i tried

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
bool only_int(string inputkey);
int check(string inputkey);
int main(int argc , string argv[])
{
    bool check = only_int(argv[1]);
   if(argc == 2)
   {
    if(check == true)
    {
        string plaintext = get_string("plaintext : ");
        return 0;
    }
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
   }
    else if((argc != 2) || (argv[1] == NULL))
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

//functions to check for only integral input
bool only_int(string inputkey)
{
    int y = check(inputkey);
    if(y > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
int check(string inputkey)
{
int result = 0;
    for(int i = 0 , l = strlen(inputkey) ; i < l ; i++)
    {
        int x = isdigit(inputkey[i]);
        if (x > 0)
        {
            result++;
        }
    }
return result;
}

i expected it to be something like this

$ ./caesar                                                                                          
Usage: ./caesar key                                                                                 
$ ./caesar HELLO                                                                                    
Usage: ./caesar key                                                                                 
$ ./caesar 1 2 3                                                                                    
Usage: ./caesar key                                                                                 
$ ./caesar 13                                                                                       
plaintext:  

but what i got

                                                                              
$ ./caesar HELLO                                                                                    
Usage: ./caesar key                                                                                 
$ ./caesar 1 2 3                                                                                    
Usage: ./caesar key
$ ./caesar 
Segmentation fault (core dumped)                                                                                 
$ ./caesar 13                                                                                       
plaintext: 

Solution

  • You're accessing argv[1] before checking the length of argv, meaning you may be accessing memory you don't own. Switch the checks around and you should be OK:

    int main(int argc , string argv[])
    {
        if (argc != 2) 
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        // Now you know you have the right number of arguments, 
        // and can safely access argv[1] and check it