Thread: fclose() causes segment fault on i686
hello
working on problem days.
program reads lines form text file , writes these lines successively buffer pointed pb_start.
program can compiled , run on 2 of x86_64 suse 9.3 machines, failed both on i686 suse9.3 , ubuntu 9.04 machines.
following program.
i run program ascii text file on i686 ubuntu 9.4 machine (linux shuttle 2.6.28-15-generic #52-ubuntu smp wed sep 9 10:49:34 utc 2009 i686 gnu/linux.)code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #define max_length 64 //assumming fixed line size of 64 characters int main(int argc, char **argv) { file *f_in; int n_line; //number of lines read file int index = 0; struct string { char data[max_length]; } *pb_start, *pb_current; //pb_start- pointer start of counter, pb_current- pointer currnet position of counter if (argc < 3) { puts("usage: command filename number_of_lines\n"); exit(0); } //open input file if (( f_in = fopen (argv[1], "r")) == null) { fprintf(stderr, "open input file failed: %s\n", strerror(errno)); exit(0); } //derive number of lines read file n_line = atoi(argv[2]); if ( (pb_start = pb_current = (struct string *) calloc(n_line, sizeof(struct string)) ) == null ) { fprintf (stderr, "calloc() failed: %s\n", strerror (errno)); exit(0); } while (fgets(pb_current->data, max_length, f_in) != null && index < n_line) { ++pb_current; ++index; } ( index = 0, pb_current = pb_start ; index < n_line; index++) { printf("%s", pb_current->data); ++pb_current; } fclose (f_in); free (pb_start); return (0); }
different n_line value chosen each test , each test repeated 1000 times. test results consistent. pending on value of n_line,
smaller n_line values generate segment fault while larger n_line values passed. if whole text read (when n_line larger or equal
lines of test file) program complete successfully.
sure calloc() , free() work fine, , believe problem comes form fopen() , fclose() part. program/library seems having difficult release memory allocated input file when file partially read.
following show gdb trace of program when segment fault take place.
[code]
program received signal sigsegv, segmentation fault.
0xb7dfcfc8 in ?? () /lib/tls/i686/cmov/libc.so.6
(gdb) bt full
#0 0xb7dfcfc8 in ?? () /lib/tls/i686/cmov/libc.so.6
no symbol table info available.
#1 0xb7dfd5b6 in free () /lib/tls/i686/cmov/libc.so.6
no symbol table info available.
#2 0xb7deafe4 in fclose () /lib/tls/i686/cmov/libc.so.6
no symbol table info available.
#3 0x080487b0 in main (argc=3, argv=0xbfd1aab4) @ t_ring_1.c:52
f_in = (file *) 0x9c9f008
n_line = 2
index = 2
pb_start = (struct string *) 0x9c9f170
pb_current = (struct string *) 0x9c9f1f0
(gdb)
planning install development version of library , trace on library see went wrong (which out of league.) so, please let me know if have experience same problem , possible solutions. comments , suggestion grateful. thanks.
putting blame on standard library wrong.
problem in code.
line problem:
it executes fgets on non existing buffer because boundary condition checked after fgetscode:while (fgets(pb_current->data, max_length, f_in) != null && index < n_line) {
evaluation of && left right (opposed evaluation of = used earlier [and bad style])
should be:
now boundary condition enforced before writingcode:while (index < n_line && fgets(pb_current->data, max_length, f_in) != null) {
missing boundary check when n_line greater lines in file while printing
add a:
after reading while loopcode:n_line = index;
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Packaging and Compiling Programs fclose() causes segment fault on i686
Ubuntu
Comments
Post a Comment