Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #8392

Re: Fread problem

From Jack <spamtrap@trashcan.invalid>
Newsgroups comp.lang.c
Subject Re: Fread problem
Date 2011-07-17 20:45 +0000
Organization Aioe.org NNTP Server
Message-ID <ivvhlc$148$1@speranza.aioe.org> (permalink)
References (1 earlier) <ivqcap$eqo$1@reader1.panix.com> <ivrl7v$3gb$1@speranza.aioe.org> <87d3har2zu.fsf@temporary-address.org.uk> <ivss0p$ulb$1@speranza.aioe.org> <1b62n23pw6.fsf@snowball.wb.pfeifferfamily.net>

Show all headers | View raw


On Sat, 16 Jul 2011 14:57:13 -0600, Joe Pfeiffer wrote:

> Jack <spamtrap@trashcan.invalid> writes:
> 
>> 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.
> 
> That's not a fix.  Your underlying bug is still present; your code is
> still broken.  Anything from adding a new line of code to a new compiler
> version to a new version of libc to slightly different input than what
> you've tested on so far could make it manifest again at any time.

I sure do understand the point everyone's making, and in an ideal world 
I'd get right to the heart of this bug. BUT... the customer pays for 
their support contract annually in advance, any time we have to spend on 
maintenance from that contract is just a cost to our company, so we get 
strong pressure from management to get quick 'n' easy fixes out the door.

Anyways, if the problem reoccurs in future, with any luck it'll end up on 
some other maintenance programmer's books not mine. :)

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Fread problem Jack <spamtrap@trashcan.invalid> - 2011-07-15 20:56 +0000
  Re: Fread problem Ian Collins <ian-news@hotmail.com> - 2011-07-16 09:05 +1200
    Re: Fread problem Noob <root@127.0.0.1> - 2011-07-19 13:41 +0200
  Re: Fread problem John Gordon <gordon@panix.com> - 2011-07-15 21:43 +0000
    Re: Fread problem Jack <spamtrap@trashcan.invalid> - 2011-07-16 09:22 +0000
      Re: Fread problem Dr Nick <3-nospam@temporary-address.org.uk> - 2011-07-16 10:25 +0100
        Re: Fread problem Jack <spamtrap@trashcan.invalid> - 2011-07-16 20:23 +0000
          Re: Fread problem Ian Collins <ian-news@hotmail.com> - 2011-07-17 08:37 +1200
          Re: Fread problem John Gordon <gordon@panix.com> - 2011-07-16 20:46 +0000
            Re: Fread problem James Kuyper <jameskuyper@verizon.net> - 2011-07-16 18:29 -0400
              Re: Fread problem John Gordon <gordon@panix.com> - 2011-07-16 23:14 +0000
          Re: Fread problem Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-16 16:55 -0400
            Re: Fread problem blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-19 17:17 +0000
              [OT] Re: Fread problem Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-19 20:11 -0400
                Re: [OT] Re: Fread problem Seebs <usenet-nospam@seebs.net> - 2011-07-20 02:27 +0000
          Re: Fread problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-16 14:57 -0600
            Re: Fread problem Jack <spamtrap@trashcan.invalid> - 2011-07-17 20:45 +0000
              Re: Fread problem Ian Collins <ian-news@hotmail.com> - 2011-07-18 08:50 +1200
              Re: Fread problem James Kuyper <jameskuyper@verizon.net> - 2011-07-17 17:06 -0400
                Re: Fread problem Ian Collins <ian-news@hotmail.com> - 2011-07-18 09:09 +1200
                Re: Fread problem Hans Vlems <hvlems@freenet.de> - 2011-07-18 13:01 -0700
              Re: Fread problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-17 16:06 -0600
              Re: Fread problem Geoff <geoff@invalid.invalid> - 2011-07-17 17:56 -0700
                Re: Fread problem Seebs <usenet-nospam@seebs.net> - 2011-07-18 04:08 +0000
                Re: Fread problem Geoff <geoff@invalid.invalid> - 2011-07-17 22:08 -0700
                Re: Fread problem Jack <spamtrap@trashcan.invalid> - 2011-07-18 20:20 +0000
                Re: Fread problem Ian Collins <ian-news@hotmail.com> - 2011-07-19 08:50 +1200
                Re: Fread problem Cat 22 <cat22@invalid.org> - 2011-07-18 18:34 -0700
                Re: Fread problem James Kuyper <jameskuyper@verizon.net> - 2011-07-19 07:19 -0400
                Re: Fread problem Stephen Sprunk <stephen@sprunk.org> - 2011-07-19 09:31 -0500
              Re: Fread problem Todd Carnes <toddcarnes@gmail.com> - 2011-07-18 04:23 +0000
              Re: Fread problem Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-07-18 01:15 -0700
              Re: Fread problem Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-18 07:22 -0400
                Re: Fread problem Kenneth Brody <kenbrody@spamcop.net> - 2011-07-18 13:56 -0400
              Re: Fread problem Kenneth Brody <kenbrody@spamcop.net> - 2011-07-18 13:49 -0400
                Re: Fread problem Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-18 20:31 -0400
                Re: Fread problem Squeamizh <squeamz@hotmail.com> - 2011-07-18 21:11 -0700
              Re: Fread problem blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-19 17:19 +0000
                Re: Fread problem Kenneth Brody <kenbrody@spamcop.net> - 2011-07-19 14:18 -0400
                Re: Fread problem pete <pfiland@mindspring.com> - 2011-07-19 15:19 -0400
                Re: Fread problem blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-19 21:33 +0000
          Re: Fread problem Geoff <geoff@invalid.invalid> - 2011-07-16 14:27 -0700
            Re: Fread problem John Gordon <gordon@panix.com> - 2011-07-16 22:22 +0000
          Re: Fread problem "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-07-16 23:28 -0700
          Re: Fread problem "J. J. Farrell" <jjf@bcs.org.uk> - 2011-07-18 04:58 +0100
          Re: Fread problem Keith Thompson <kst-u@mib.org> - 2011-07-18 08:53 -0700
            Re: Fread problem Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-18 18:07 +0100
              Re: Fread problem Keith Thompson <kst-u@mib.org> - 2011-07-18 12:50 -0700
            Re: Fread problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-18 12:18 -0600
            Re: Fread problem John Gordon <gordon@panix.com> - 2011-07-18 20:18 +0000
              Re: Fread problem Keith Thompson <kst-u@mib.org> - 2011-07-18 13:28 -0700
                Re: Fread problem Lew Pitcher <lpitcher@teksavvy.com> - 2011-07-18 16:55 -0400
              Re: Fread problem Anders Wegge Keller <wegge@wegge.dk> - 2011-07-18 22:51 +0200
            Re: Fread problem blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-19 17:21 +0000
          Re: Fread problem Kenneth Brody <kenbrody@spamcop.net> - 2011-07-18 13:44 -0400
      Re: Fread problem Ian Collins <ian-news@hotmail.com> - 2011-07-16 21:39 +1200
      Re: Fread problem Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-16 12:18 -0600
      Re: Fread problem John Gordon <gordon@panix.com> - 2011-07-16 19:36 +0000

csiph-web