Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling isalpha Causing Segmentation Fault

I have the following program that causes a segmentation fault.

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

int main(int argc, char *argv[])
{
    printf("TEST");

    for (int k=0; k<(strlen(argv[1])); k++)
    {
        if (!isalpha(argv[1])) {
            printf("Enter only alphabets!");
            return 1;
        }
    }

    return 0;
}

I've figured out that it is this line that is causing the problem

if (!isalpha(argv[1])) {

and replacing argv[1] with argv[1][k] solves the problem.

However, I find it rather curious that the program results in a segmentation fault without even printing TEST. I also expect the isalpha function to incorrectly check if the lower byte of the char* pointer to argv[1], but this doesn't seem to be the case. I have code to check for the number of arguments but isn't shown here for brevity.

What's happening here?

like image 590
Mohideen Imran Khan Avatar asked May 28 '18 10:05

Mohideen Imran Khan


People also ask

What triggers segmentation fault?

A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

Is segmentation fault a compiler error?

Segfault is a run-time error, not compiler.

What is signal segmentation fault?

On a Unix operating system such as Linux, a "segmentation violation" (also known as "signal 11", "SIGSEGV", "segmentation fault" or, abbreviated, "sig11" or "segfault") is a signal sent by the kernel to a process when the system has detected that the process was attempting to access a memory address that does not ...


1 Answers

In general it is rather pointless to discuss why undefined behaviour leads to this result or the other.

But maybe it doesn't harm to try to understand why something happens even if it is outside the spec.

There are implementation of isalpha which use a simple array to lookup all possible unsigned char values. In that case the value passed as parameter is used as index into the array. While a real character is limited to 8 bits, an integer is not. The function takes an int as parameter. This is to allow entering EOF as well which does not fit into unsigned char.

If you pass an address like 0x7239482342 into your function this is far beyond the end of the said array and when the CPU tries to read the entry with that index it falls off the rim of the world. ;)

Calling isalpha with such an address is the place where the compiler should raise some warning about converting a pointer to an integer. Which you probably ignore...

The library might contain code that checks for valid parameters but it might also just rely on the user not passing things that shall not be passed.

like image 124
Gerhardh Avatar answered Oct 21 '22 19:10

Gerhardh