Calling isalpha Causing Segmentation Fault

  • A+
Category:Languages

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?


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: