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


Groups > comp.lang.c > #61712

Re: Method other than strlen to check for truncation with fgets?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Method other than strlen to check for truncation with fgets?
Date 2015-05-07 12:00 -0700
Organization None to speak of
Message-ID <878ud0jjri.fsf@kst-u.example.com> (permalink)
References <midqe0$62n$1@dont-email.me> <chine.bleu-B62ADF.11305507052015@news.eternal-september.org>

Show all headers | View raw


Siri Cruz <chine.bleu@yahoo.com> writes:
> In article <midqe0$62n$1@dont-email.me>, mathog <dmathog@gmail.com> wrote:
>> It should work when the input line is truncated.  The problem is that,
>> from my reading of the fgets man page, there is no guarantee that the 
>> library will not write something beyond the \0 in the buffer.  I don't 
>> expect that would happen, but also do not see anything saying that it 
>> cannot.
>
> If this is critical to you, use fgetline instead.
>
> /*
>     Read and return the next line from input.
>     The line is mallocked; caller is responsibile for freeing.
>     The line ends in \n if the line from the input file did.
>     Returns NULL on EOF.
> */
>
> char *fgetline (FILE *input) {
>     int c; char *line = NULL; int m = 0, n = 0;
>     while ((c=fgetc(input))!=EOF) {
>         if (c==0) abort();
>         if (n+1>=m) {m = 2*(n+1); line = realloc(line, m);}
>         if (!line) abort();
>         line[n++] = c;
>         if (c=='\n') break;
>     }
>     if (line) {line = realloc(line, n+1); line[n] = 0;}
>     return line;
> }

This doesn't check for allocation failures.

-- 
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


Thread

Method other than strlen to check for truncation with fgets? mathog <dmathog@gmail.com> - 2015-05-06 12:35 -0700
  Re: Method other than strlen to check for truncation with fgets? Barry Schwarz <schwarzb@dqel.com> - 2015-05-06 15:14 -0700
    Re: Method other than strlen to check for truncation with fgets? mathog <dmathog@gmail.com> - 2015-05-06 16:14 -0700
      Re: Method other than strlen to check for truncation with fgets? Barry Schwarz <schwarzb@dqel.com> - 2015-05-06 17:51 -0700
      Re: Method other than strlen to check for truncation with fgets? Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-06 18:23 -0700
        Re: Method other than strlen to check for truncation with fgets? mathog <dmathog@gmail.com> - 2015-05-07 10:07 -0700
          Re: Method other than strlen to check for truncation with fgets? Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-07 16:06 -0700
  Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-06 22:20 -0400
    Re: Method other than strlen to check for truncation with fgets? mathog <dmathog@gmail.com> - 2015-05-07 10:29 -0700
      Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-07 14:09 -0400
        Re: Method other than strlen to check for truncation with fgets? mathog <dmathog@gmail.com> - 2015-05-07 12:48 -0700
          Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-07 17:01 -0400
            Re: Method other than strlen to check for truncation with fgets? mathog <dmathog@gmail.com> - 2015-05-08 10:59 -0700
              Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-08 14:48 -0400
              Re: Method other than strlen to check for truncation with fgets? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-08 18:37 +0000
                Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-08 15:06 -0400
  Re: Method other than strlen to check for truncation with fgets? Lőrinczy Zsigmond <nospam@for.me> - 2015-05-07 07:04 +0200
    Re: Method other than strlen to check for truncation with fgets? Richard Heathfield <rjh@cpax.org.uk> - 2015-05-07 06:52 +0100
      Re: Method other than strlen to check for truncation with fgets? Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-05-07 09:58 +0200
        Re: Method other than strlen to check for truncation with fgets? Stephen Sprunk <stephen@sprunk.org> - 2015-05-07 09:12 -0500
        Re: Method other than strlen to check for truncation with fgets? Keith Thompson <kst-u@mib.org> - 2015-05-07 08:51 -0700
          Re: Method other than strlen to check for truncation with fgets? Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-05-07 19:09 +0200
            Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-07 14:15 -0400
    Re: Method other than strlen to check for truncation with fgets? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-07 11:31 +0100
      Re: Method other than strlen to check for truncation with fgets? Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-05-07 15:41 +0200
        Re: Method other than strlen to check for truncation with fgets? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-08 02:44 +0100
          Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-07 23:24 -0400
            Re: Method other than strlen to check for truncation with fgets? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-08 04:41 +0100
      Re: Method other than strlen to check for truncation with fgets? Keith Thompson <kst-u@mib.org> - 2015-05-07 08:49 -0700
        Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-07 12:19 -0400
        Re: Method other than strlen to check for truncation with fgets? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-07 18:08 +0000
    Re: Method other than strlen to check for truncation with fgets? James Kuyper <jameskuyper@verizon.net> - 2015-05-07 12:19 -0400
      Re: Method other than strlen to check for truncation with fgets? mathog <dmathog@gmail.com> - 2015-05-07 09:57 -0700
  Re: Method other than strlen to check for truncation with fgets? Siri Cruz <chine.bleu@yahoo.com> - 2015-05-07 11:31 -0700
    Re: Method other than strlen to check for truncation with fgets? Keith Thompson <kst-u@mib.org> - 2015-05-07 12:00 -0700
      Re: Method other than strlen to check for truncation with fgets? Siri Cruz <chine.bleu@yahoo.com> - 2015-05-07 12:44 -0700
        Re: Method other than strlen to check for truncation with fgets? Keith Thompson <kst-u@mib.org> - 2015-05-07 14:03 -0700
          Re: Method other than strlen to check for truncation with fgets? Siri Cruz <chine.bleu@yahoo.com> - 2015-05-07 15:28 -0700

csiph-web