SIGSEGV with C compiler/RTL. Possible scanf() problem.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dear All,
	I have been experiencing variable corruption / SIGSEGV problems with 
some pieces of quite simple code.  The exact symptoms are hard to pin down 
and can vary from gcc version to version and whether or not compiler 
optimisation is turned on.  Trivial changes to the program can make the 
symptoms come and go.  The common factor is a particular scanf() statement.  
Here is an illustration which generates a SIGSEGV when the EOF is read.  I 
am entering input data "123.456,55<CR><ctrl-D>" from the keyboard.  The 
results are identical if redirecting stdin from a file.  Can anyone 
reproduce this error and/or fault my code?

Thanks
Tom Crane.

Ps. The code below was tried on another machine with the same gcc version 
and gave the same SIGSEGV so I probably don't have a physical memory 
problem.
------------------------------------------------------------------------------
$ uname -a
Linux asus 2.6.20 #7 Wed Sep 5 03:23:16 BST 2007 i686 athlon-4 i386 GNU/Linux
$ gcc -v
Reading specs from /usr/lib/gcc/i486-slackware-linux/4.1.2/specs
Target: i486-slackware-linux
Configured with: ../gcc-4.1.2/configure --prefix=/usr --enable-shared 
--enable-languages=ada,c,c++,fortran,java,objc --enable-threads=posix 
--enable-__cxa_atexit --disable-checking --with-gnu-ld --verbose 
--with-arch=i486 --target=i486-slackware-linux --host=i486-slackware-linux
Thread model: posix
gcc version 4.1.2

$ cat tmp5.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  double offset, caltime;
  unsigned int sigbits;
  unsigned char ignore;
  
  offset=0.0;
  while (scanf("%20lf%[,]%u", &caltime, &ignore, &sigbits) != EOF) {
    caltime=caltime-offset;
    printf ("caltime=%lf sigbits=%u\n", caltime, sigbits);
  }
  return 0;
}

$ cc -Wall -O2 tmp5.c
tom@asus:tmp$ a.out
123.456,55
caltime=123.456000 sigbits=55
Segmentation fault

$ cc -Wall tmp5.c
$ a.out
123.456,55
caltime=123.456000 sigbits=55
<no segfault occurs here>
-- 
Tom Crane, Dept. Physics, Royal Holloway, University of London, Egham Hill,
Egham, Surrey, TW20 0EX, England. 
Email:  T.Crane@xxxxxxxxxx
Fax:    +44 (0) 1784 472794


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux