Re: segmentation fault isdigit() cuasing the problem.

2009-07-06 Thread Jean Christophe Beyler
This is not a bug.

In the man page of the isdigit:

These functions check whether c, which must have the value of an
unsigned char or EOF, falls into a certain  character class according
to the current locale.

You should first test your value with a isascii if using isdigit.


Your code has a major memory overflow risk:
scanf(%d, devisor);


1) Your test is wrong:

while(isdigit(devisor)); does not do what you think.

When you are giving it 21, it takes that code and says : the ASCII
code 22 is not a digital value in the ASCII table.

2) scanf is really a dangerous function, because of this overflow
issue, bad error handling.
  - better is scanf with a %s in a buffer that you parse
afterwards with strtol for example (good for error testing)
  - even better, fgets which allows to minimize the bugs of your
code by allowing error management.

Jc

On Thu, Jul 2, 2009 at 1:55 AM, vartanvartan...@att.net wrote:
 dear maintainer:
 the fallowing is a small sample of the code that contains all the info
 to reproduce the problem.
 the C library function isdigit() fails and causes segmention fault when
 the input is is not numeric like xx.

 COMPILE COMMAND :  gcc -ansi --version -Wall  -o ch2-13.out    -lm
 ch2-13.c
 gcc (Debian 4.3.2-1.1) 4.3.2
 Copyright (C) 2008 Free Software Foundation, Inc



segmentation fault isdigit() cuasing the problem.

2009-07-01 Thread vartan
dear maintainer:
the fallowing is a small sample of the code that contains all the info
to reproduce the problem.
the C library function isdigit() fails and causes segmention fault when
the input is is not numeric like xx.

COMPILE COMMAND :  gcc -ansi --version -Wall  -o ch2-13.out-lm
ch2-13.c
gcc (Debian 4.3.2-1.1) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc
/* USER NAME : vartan
 * DATE STARTED : Wed Jul 01 19:28:34 2009
 *
 * COMPILE COMMAND : gcc -ansi --version -Wall  -o ch2-13.out-lm ch2-13.c */

/* INCLUDE FILES */

#include stdio.h
#include math.h
#include stdlib.h
#include ctype.h

/* PREPROCESSOR CONSTANTS */


 int main(void){ /* start of main */

 int  i = 1,
  devisor, j = 0;
 
 do {
 
printf(Please enter a valid numeric for devisor\n);
scanf(%d, devisor);
 }
 while (isdigit(devisor));
 
  
 printf (counting\n);
 while (i = 100)
 {
   if (!( i++ % devisor))
   {
  printf(%4d,(i-1));/* point to the correct line number that is devidable by devisor*/
  printf( *\n);
  j++;
   }
   else
   ;

 }  /* end of while */
   printf(\n The number of lines that is dividable by %d was %d.\n, devisor,j);

  return(0);

 } /* end of main */