H
hzmonte
My C program has the following:
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
....
}
strace has the following:
open("/es/m/.messengers_profile", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0600, st_size=122, ...}) = 0
mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb75ee000
read(3, "key = 1244\nlib = /lib/libc.so.6\n"..., 32768) = 122
....
write(1, "in skip start fp=0x9ded0f8 line="..., 39in skip start
fp=0x9ded0f8 line=6 c=32) = 39
write(1, "i=0\n", 4i=0) = 4
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
It appears that the C runtime reads all the file's 122 characters in a
cache with starting address 0xb75ee000. And my C program continues to
read (i.e. getc) from this cache. Eventually, when it reaches EOF when
executing the getc(fp) in skip_space(), it crashes.
Interestingly, running it with gdb does not crash. I am using gcc
3.2.3 on Red Hat Linux kernel 2.4. How can I find out what's wrong?
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
....
}
strace has the following:
open("/es/m/.messengers_profile", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0600, st_size=122, ...}) = 0
mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb75ee000
read(3, "key = 1244\nlib = /lib/libc.so.6\n"..., 32768) = 122
....
write(1, "in skip start fp=0x9ded0f8 line="..., 39in skip start
fp=0x9ded0f8 line=6 c=32) = 39
write(1, "i=0\n", 4i=0) = 4
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
It appears that the C runtime reads all the file's 122 characters in a
cache with starting address 0xb75ee000. And my C program continues to
read (i.e. getc) from this cache. Eventually, when it reaches EOF when
executing the getc(fp) in skip_space(), it crashes.
Interestingly, running it with gdb does not crash. I am using gcc
3.2.3 on Red Hat Linux kernel 2.4. How can I find out what's wrong?