Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Reading beyond the end of file |
| Date | 2015-08-18 11:17 -0700 |
| Organization | None to speak of |
| Message-ID | <lnbne44g1k.fsf@kst-u.example.com> (permalink) |
| References | <b62ba036-de31-4f37-aaad-3d1372d40d3f@googlegroups.com> <55D277F4.5090905@verizon.net> <691ee45c-f295-4c1d-bad8-7a7cde1f5142@googlegroups.com> <fem6ta10i0pa2abn5peflm0gergf5091fm@4ax.com> |
Barry Schwarz <schwarzb@dqel.com> writes:
> On Tue, 18 Aug 2015 00:54:48 -0700 (PDT), janus <emekamicro@gmail.com>
> wrote:
>
>> R = fopen(fn, "r");
>> printf("Word Count: %d\n", meanSentenceLength(R));
>> if (!R) {
>> printf(fn);
>> printf("\n");
>> cerror(0, "Failed to open source file");
>> }
>
> Since other messages show that meanSentenceLength attempts to use R to
> read a file, your if statement occurs too late. If fn fails to open
> for any reason, R will be set to NULL. meanSentenceLength will then
> cause undefined behavior by attempting to read from an invalid FILE*.
> If you are going to test the result of a function for success (and you
> definitely should in this case), then that test needs to occur before
> you try to use the result to do some "real" processing.
That's very likely true, but I don't think we've seen the implementation
of meanSentenceLength() (at least not in any direct parent of this
article), and it *might* handle a null pointer gracefully.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-17 16:24 -0700
Re: Reading beyond the end of file Bartc <bc@freeuk.com> - 2015-08-18 00:53 +0100
Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-08-17 18:44 -0700
Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 14:41 -0700
Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-09-06 17:09 -0700
Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-08-17 20:10 -0400
Re: Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-18 00:54 -0700
Re: Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-18 01:43 -0700
Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-08-18 10:18 -0400
Re: Reading beyond the end of file Barry Schwarz <schwarzb@dqel.com> - 2015-08-18 09:18 -0700
Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-08-18 11:17 -0700
Re: Reading beyond the end of file Barry Schwarz <schwarzb@dqel.com> - 2015-08-18 13:24 -0700
csiph-web