[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Fread problem

Jack

7/15/2011 8:57:00 PM

Hi,

I got a strange problem with Fread:

It crashes my program from time to time, in one of its internal memcpy
calls.

I managed to bypass this bug one time by creating a bigger file buffer,
but it's just a patch...

Does anyone knows anything about it?
Is there a clean way to reset the file buffers?
Can this be compiler dependent (gcc 4.1) - maybe a compiler bug?

Thanks for any help
57 Answers

Ian Collins

7/15/2011 9:05:00 PM

0

On 07/16/11 08:56 AM, Jack wrote:
> Hi,
>
> I got a strange problem with Fread:
>
> It crashes my program from time to time, in one of its internal memcpy
> calls.

Assuming it is fread(), it is very unlikely the function is the cause of
the problem. The much more likely cause is something else in your
program stomping on the bugger space.

Run the program with an bounds checker (such is Valgrind in Linux).


--
Ian Collins

John Gordon

7/15/2011 9:44:00 PM

0

In <ivq9i7$841$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid> writes:

> I got a strange problem with Fread:

> It crashes my program from time to time, in one of its internal memcpy
> calls.

When things like this happen it is nearly always because of a memory
corruption bug elsewhere in your program, not because of a bug in a
library function like fread().

C is, unfortunately, a fragile language. If you copy more to a buffer
than it can hold, or call free() on a bad value, or call free() twice on
the same value, you've just corrupted the memory space of your program and
anything can happen from that point onward, including crashes in library
functions such as fread().

If your program is a simple one, go over it again and try to identify any
areas where you might have committed the above errors. If it's a complex
program, you might use a memory analyzer such as efence or valgrind.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Jack

7/16/2011 9:22:00 AM

0

On Fri, 15 Jul 2011 21:43:53 +0000, John Gordon wrote:
> In <ivq9i7$841$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid>
> writes:
>
>> I got a strange problem with Fread:
>
>> It crashes my program from time to time, in one of its internal memcpy
>> calls.
>
> When things like this happen it is nearly always because of a memory
> corruption bug elsewhere in your program, not because of a bug in a
> library function like fread().
>
> C is, unfortunately, a fragile language. If you copy more to a buffer
> than it can hold, or call free() on a bad value, or call free() twice on
> the same value, you've just corrupted the memory space of your program
> and anything can happen from that point onward, including crashes in
> library functions such as fread().
>
> If your program is a simple one, go over it again and try to identify
> any areas where you might have committed the above errors. If it's a
> complex program, you might use a memory analyzer such as efence or
> valgrind.

Yes...
Actually, I think it's a memory error somewhere that causes really
indirectly this bug.
But I find it strange that even a memory error makes a fread with proper
parameters ( I do check this ) fail.

I've heard somewhere one can play with the file buffer, so I tried it,
and put MY file buffer, with the Setbuff() function. And it didn't crash
anymore, but the read couldn't read all the files. Some bytes in the
middle were missing (I think it is when I resume the reading, after
having processed the first part)...

Last thing, this Fread occurs in a big source file... Created by lex /
yacc.

The file is big, not easy to understand, so difficult to debug
: (

Nick

7/16/2011 9:26:00 AM

0

Jack <spamtrap@trashcan.invalid> writes:

> On Fri, 15 Jul 2011 21:43:53 +0000, John Gordon wrote:
>> In <ivq9i7$841$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid>
>> writes:
>>
>>> I got a strange problem with Fread:
>>
>>> It crashes my program from time to time, in one of its internal memcpy
>>> calls.
>>
>> When things like this happen it is nearly always because of a memory
>> corruption bug elsewhere in your program, not because of a bug in a
>> library function like fread().
>>
>> C is, unfortunately, a fragile language. If you copy more to a buffer
>> than it can hold, or call free() on a bad value, or call free() twice on
>> the same value, you've just corrupted the memory space of your program
>> and anything can happen from that point onward, including crashes in
>> library functions such as fread().
>>
>> If your program is a simple one, go over it again and try to identify
>> any areas where you might have committed the above errors. If it's a
>> complex program, you might use a memory analyzer such as efence or
>> valgrind.
>
> Yes...
> Actually, I think it's a memory error somewhere that causes really
> indirectly this bug.
> But I find it strange that even a memory error makes a fread with proper
> parameters ( I do check this ) fail.

Somewhere the library and the operating system have to store things like
the details of the underlying file, the position in it etc. When you
trample over memory you can overwrite this.

In particular, many systems get the buffers for the stdio streams from
the same memory pool as you get with malloc. So do something wrong with
a block you've got from malloc and you can really mess the streams up.

And vice versa. I once managed to get malloc to return "null" by
previously fclosing the same file pointer twice.

> I've heard somewhere one can play with the file buffer, so I tried it,
> and put MY file buffer, with the Setbuff() function. And it didn't crash
> anymore, but the read couldn't read all the files. Some bytes in the
> middle were missing (I think it is when I resume the reading, after
> having processed the first part)...

That's not incompatible with what I wrote above.
--
Online waterways route planner | http://ca...
Plan trips, see photos, check facilities | http://canalp...

Ian Collins

7/16/2011 9:40:00 AM

0

On 07/16/11 09:22 PM, Jack wrote:
> On Fri, 15 Jul 2011 21:43:53 +0000, John Gordon wrote:
>> In<ivq9i7$841$1@speranza.aioe.org> Jack<spamtrap@trashcan.invalid>
>> writes:
>>
>>> I got a strange problem with Fread:
>>
>>> It crashes my program from time to time, in one of its internal memcpy
>>> calls.
>>
>> When things like this happen it is nearly always because of a memory
>> corruption bug elsewhere in your program, not because of a bug in a
>> library function like fread().
>>
>> C is, unfortunately, a fragile language. If you copy more to a buffer
>> than it can hold, or call free() on a bad value, or call free() twice on
>> the same value, you've just corrupted the memory space of your program
>> and anything can happen from that point onward, including crashes in
>> library functions such as fread().
>>
>> If your program is a simple one, go over it again and try to identify
>> any areas where you might have committed the above errors. If it's a
>> complex program, you might use a memory analyzer such as efence or
>> valgrind.
>
> Yes...
> Actually, I think it's a memory error somewhere that causes really
> indirectly this bug.

So try valgrind...

> But I find it strange that even a memory error makes a fread with proper
> parameters ( I do check this ) fail.

Why strange? All the memory is in one address space, so any part of the
program can mess with the buffers being used by stdio.

--
Ian Collins

Joe Pfeiffer

7/16/2011 6:18:00 PM

0

Jack <spamtrap@trashcan.invalid> writes:

> On Fri, 15 Jul 2011 21:43:53 +0000, John Gordon wrote:
>> In <ivq9i7$841$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid>
>> writes:
>>
>>> I got a strange problem with Fread:
>>
>>> It crashes my program from time to time, in one of its internal memcpy
>>> calls.
>>
>> When things like this happen it is nearly always because of a memory
>> corruption bug elsewhere in your program, not because of a bug in a
>> library function like fread().
>>
>> C is, unfortunately, a fragile language. If you copy more to a buffer
>> than it can hold, or call free() on a bad value, or call free() twice on
>> the same value, you've just corrupted the memory space of your program
>> and anything can happen from that point onward, including crashes in
>> library functions such as fread().
>>
>> If your program is a simple one, go over it again and try to identify
>> any areas where you might have committed the above errors. If it's a
>> complex program, you might use a memory analyzer such as efence or
>> valgrind.
>
> Yes...
> Actually, I think it's a memory error somewhere that causes really
> indirectly this bug.
> But I find it strange that even a memory error makes a fread with proper
> parameters ( I do check this ) fail.

Why should this be any surprise at all? It looks like you're corrupting
memory, so the buffers fread() depends on are failing.

> I've heard somewhere one can play with the file buffer, so I tried it,
> and put MY file buffer, with the Setbuff() function. And it didn't crash
> anymore, but the read couldn't read all the files. Some bytes in the
> middle were missing (I think it is when I resume the reading, after
> having processed the first part)...

Sounds more and more like a wild pointer someplace.

> Last thing, this Fread occurs in a big source file... Created by lex /
> yacc.
>
> The file is big, not easy to understand, so difficult to debug
> : (

Start by assuming yacc and lex are working correctly, do your debugging
on the yacc and lex source files, and on whatever utility C functions
you've defined. lex and yacc output isn't meant to be human readable;
trying to understand what they're putting out is really useless for
debugging purposes.

John Gordon

7/16/2011 7:36:00 PM

0

In <ivrl7v$3gb$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid> writes:

> But I find it strange that even a memory error makes a fread with proper
> parameters ( I do check this ) fail.

Unfortunately, it has nothing to do with the parameters to fread() being
right or wrong. Once you corrupt the underlying memory, any function can
go wrong.

Here's an analogy:

Imagine you're a cook at a restaurant. The restaurant has lots of recipes
that have been tested and you know they work.

One day, a customer orders pancakes. You cook them up and serve them.
However, the cook on the previous shift didn't seal the flour container
properly, so the flour had weevils in it. The pancakes are rotten and
the customer gets sick.

Nothing was wrong with the recipe (the function arguments); you did
everything right. But an ingredient (memory) was corrupted and so the
function crashes (pancakes are rotten).

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Jack

7/16/2011 8:24:00 PM

0

On Sat, 16 Jul 2011 10:25:41 +0100, Dr Nick wrote:

> Jack <spamtrap@trashcan.invalid> writes:
>
>> On Fri, 15 Jul 2011 21:43:53 +0000, John Gordon wrote:
>>> In <ivq9i7$841$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid>
>>> writes:
>>>
>>>> I got a strange problem with Fread:
>>>
>>>> It crashes my program from time to time, in one of its internal
>>>> memcpy calls.
>>>
>>> When things like this happen it is nearly always because of a memory
>>> corruption bug elsewhere in your program, not because of a bug in a
>>> library function like fread().
>>>
>>> C is, unfortunately, a fragile language. If you copy more to a buffer
>>> than it can hold, or call free() on a bad value, or call free() twice
>>> on the same value, you've just corrupted the memory space of your
>>> program and anything can happen from that point onward, including
>>> crashes in library functions such as fread().
>>>
>>> If your program is a simple one, go over it again and try to identify
>>> any areas where you might have committed the above errors. If it's a
>>> complex program, you might use a memory analyzer such as efence or
>>> valgrind.
>>
>> Yes...
>> Actually, I think it's a memory error somewhere that causes really
>> indirectly this bug.
>> But I find it strange that even a memory error makes a fread with
>> proper parameters ( I do check this ) fail.
>
> Somewhere the library and the operating system have to store things like
> the details of the underlying file, the position in it etc. When you
> trample over memory you can overwrite this.
>
> In particular, many systems get the buffers for the stdio streams from
> the same memory pool as you get with malloc. So do something wrong with
> a block you've got from malloc and you can really mess the streams up.
>
> And vice versa. I once managed to get malloc to return "null" by
> previously fclosing the same file pointer twice.
>
>> I've heard somewhere one can play with the file buffer, so I tried it,
>> and put MY file buffer, with the Setbuff() function. And it didn't
>> crash anymore, but the read couldn't read all the files. Some bytes in
>> the middle were missing (I think it is when I resume the reading, after
>> having processed the first part)...
>
> That's not incompatible with what I wrote above.

Thanks for your help. I think I've now found a solution.

After the file is first Fopen'd, I make a backup of the internal buffers.
fp=Fopen(...);
FILE fback;
memcpy(&fback,fp,sizeof(FILE));

then before the fread I restore this buffer,
memcpy(fp,&fback,sizeof(FILE));
Fread(...,fp);

Now there is no segmentation fault, job done.

Ian Collins

7/16/2011 8:38:00 PM

0

On 07/17/11 08:23 AM, Jack wrote:

> Thanks for your help. I think I've now found a solution.

No, you have simply moved the problem elsewhere.

> After the file is first Fopen'd, I make a backup of the internal buffers.
> fp=Fopen(...);
> FILE fback;
> memcpy(&fback,fp,sizeof(FILE));
>
> then before the fread I restore this buffer,
> memcpy(fp,&fback,sizeof(FILE));
> Fread(...,fp);
>
> Now there is no segmentation fault, job done.

Until the bug you have yet to fix pops up in another random place.

--
Ian Collins

John Gordon

7/16/2011 8:47:00 PM

0

In <ivss0p$ulb$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid> writes:

> Thanks for your help. I think I've now found a solution.

> After the file is first Fopen'd, I make a backup of the internal buffers.
> fp=Fopen(...);
> FILE fback;
> memcpy(&fback,fp,sizeof(FILE));

> then before the fread I restore this buffer,
> memcpy(fp,&fback,sizeof(FILE));
> Fread(...,fp);

> Now there is no segmentation fault, job done.

If that works, it's purely accidental and I wouldn't count on it.

Your code even has a bug: you're copying sizeof(FILE) bytes, but
fp is a pointer-to-FILE which is probably not the same size.


--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"