Subject: Re: Type coersion long->int ?
To: Patrick Welche <prlw1@newn.cam.ac.uk>
From: Peter Seebach <seebs@plethora.net>
List: current-users
Date: 02/02/2000 12:39:05
In message <20000202182206.A783@quartz.newn.cam.ac.uk>, Patrick Welche writes:
>On i386, I reliably get a segmentation fault with the following program:
>
>#include <ctype.h>
>#include <stdio.h>
>#include <stdlib.h>
>
>int main()
>{
>  int c;
>  srandom(42);
>  c=(int)random(); /* random returns long */
>  if(isalpha(c))   /* isalpha takes int   */
>    printf("%c",c);
>
>  return 0;
>} 
>
>The fault happens at if(isalpha(c)). Am I actually doing something illegal,
>or is there a problem here?

Your fault.  The argument to 'isalpha' is 'int', yes, but it is required to
be in the range [0,UCHAR_MAX] or the negative value EOF.  (I believe our EOF
is -1, but be aware that a particularly hostile implementor could core dump
on -1, and accept EOF, which could be -2, for instance.)

Pretty much everything in libc that takes an "int" for a char requires the
same range of inputs.

7.3 (<ctype.h> spec in C89/C90) says
	In all cases the argument is an int, the value of which shall be
	representable as an unsigned char or shall equal the value of the
	macro EOF.  If the argument has any other value, the behavior is
	undefined.

You're passing 1413335460, which is not a valid argument.

-s