Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #390334
| From | Michael S <already5chosen@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Buffer contents well-defined after fgets() reaches EOF ? |
| Date | 2025-02-15 20:06 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <20250215200602.00004ac2@yahoo.com> (permalink) |
| References | (7 earlier) <86ldu9zxkb.fsf@linuxsc.com> <20250214165108.00002984@yahoo.com> <20250214085627.815@kylheku.com> <871pw073if.fsf@nosuchdomain.example.com> <20250214112225.480@kylheku.com> |
On Fri, 14 Feb 2025 19:34:59 -0000 (UTC) Kaz Kylheku <643-408-1753@kylheku.com> wrote: > On 2025-02-14, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > > Kaz Kylheku <643-408-1753@kylheku.com> writes: > > [...] > >> It would be good if fgets nuked the terminating newline. > >> > >> Many uses of fgets, after every operation, look for the newline > >> and nuke it, before doing anything else. > >> > >> There is a nice idiom for that, by the way, which avoids an > >> temporary variable and if test: > >> > >> line[strcspn(line, "\n")] = 0; > > [...] > > > > Then how do you detect a partial line? That can occur either if > > the last line doesn't have a terminating newline (on systems that > > permit it) or a line that's too long to fit in the array. > > I've seen many programs like this don't care. They have some > 'char buf[4096]' and that's that. > IMHO, even a program that is not designed to handle long lines should give an informative error diagnostic when it encounters one. Your trick described below is good for that, but one has to be rather good programmer in order to invent such trick. I believe that if one has to be good in order to use the API it's a clear indication that the API is *not* good. Similarly, IMHO, the programs not designed to handle presence of null characters in the text should give an informative error diagnostic when they are encountered. And in that fgets() is especially unhelpful. > In a program not required or designed to handle arbitrarily > long lines, you can do something very simple (prior to the > above line[strcspn(line, "\n")] = 0 expression). > > - zero-initialize the buffer. > > - after every call to fgets, inspect the value of the second-to-last > array element. If the value is neither zero, nor '\n', then somehow > diagnose that a too-long line has been presented to the program, > contrary to its documented limitations. > > This will yield a false positive on an unterminated last line. That > issue can be added as a documented limitation, or else the buffer can > be sized one greater than what the documented line length limit > requires, so that the program allows inner lines to be one character > longer than the documented limit, but is strict with regard to an > unterminated last line. >
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 06:59 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-09 06:23 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-08 22:23 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-08 23:12 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-09 23:52 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 17:06 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 17:22 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-10 12:49 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-13 07:14 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-14 16:51 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 15:10 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-14 17:23 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 16:46 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 17:28 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 17:22 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-14 11:03 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 19:34 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:06 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 20:01 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 20:51 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:02 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:29 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:29 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-16 01:04 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-16 07:37 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 18:59 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-16 10:48 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:14 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-17 11:54 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-16 07:32 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-16 11:05 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:25 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:21 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-16 20:26 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:41 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:29 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:33 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 20:23 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-14 14:38 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 21:02 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:53 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:48 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-15 08:37 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:08 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-18 20:17 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-21 05:58 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-15 08:12 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:44 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-10 03:28 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 20:11 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-10 07:21 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-10 16:39 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-10 13:58 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 01:03 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-10 22:33 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 03:42 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 04:54 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 13:07 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 21:47 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 17:44 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-12 06:16 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-11 13:59 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 17:58 -0500
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-11 15:40 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-17 21:11 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-10 22:32 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-10 22:38 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 12:04 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-13 07:29 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 08:13 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-09 12:50 +0200
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 18:29 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 21:57 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 07:27 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 18:34 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 16:57 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:35 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 20:37 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 07:08 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 22:41 -0800
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 07:54 +0100
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 23:22 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 00:59 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-17 02:50 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 22:57 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 23:24 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Ben Bacarisse <ben@bsb.me.uk> - 2025-02-10 01:32 +0000
Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:40 +0100
csiph-web