unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Arsen Arsenović via Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: rms@gnu.org, 9800@debbugs.gnu.org
Subject: bug#9800: Incomplete truncated file buffers from the /proc filesystem
Date: Sun, 12 Feb 2023 11:21:29 +0100	[thread overview]
Message-ID: <861qmvcglp.fsf@aarsen.me> (raw)
In-Reply-To: <4EA5E08D.8070903@cs.ucla.edu>

[-- Attachment #1: Type: text/plain, Size: 1655 bytes --]

Hi,

I was just debugging this before I found the bug report.  The diagnosis
is right: st_size is wrong for proc files (and, I'd argue, for regular
files sometimes).  So, I agree with Paul.

Paul Eggert <eggert@cs.ucla.edu> writes:

> On 10/24/11 14:50, Richard Stallman wrote:
>> I think there was a reason for doing it this way.  Perhaps so as to
>> allocate the space before reading the file.
>
> Yes, that sounds right.  And in the typical case where the file is not
> growing, that allocates space efficiently.  If the file is growing, though,
> it's OK to allocate more space after discovering that the initial
> allocation was too small.

Right.  The best possible approach is, likely:

  fstat (fd, x, &st)
  bufsz = max (READ_BUF_SIZE, st.st_size)
  buf = malloc (bufsz)

  int ret = 0, readsz = 0;
  do
    {
      readsz += ret;
      if (readsz == bufsz && size isn't unreasonable)
        {
          /* value chosen arbitrarily.  */
          bufsz += min (16 * READ_BUF_SIZE, bufsz)
          buf = realloc (buf, bufsz)
        }
      errno = 0
      ret = read (fd, buf + readsz, bufsz - readsz)
    }
  while (ret > 0 || errno == EINTR);

... or such.  This approach is robust and general, and I suspect it'd
even work for named pipes.

st_size isn't a good enough indicator of size, and it can go out of date
before TOU, however, it's - no doubt - a useful hint in the 99% case.
Using st_size to figure out a base allocation size and extending
appropriately is a well known strategy, and it would be appropriate to
do so here.

Thanks in advance, have a great day.
-- 
Arsen Arsenović

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 381 bytes --]

  reply	other threads:[~2023-02-12 10:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-19 22:59 bug#9800: Incomplete truncated file buffers from the /proc filesystem Juri Linkov
2011-10-20  8:22 ` Eli Zaretskii
2011-10-20  8:44   ` Andreas Schwab
2023-02-12  7:38     ` Eli Zaretskii
2023-02-12  9:24       ` Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2011-10-24  2:53 ` Paul Eggert
2011-10-24 21:50   ` Richard Stallman
2011-10-24 22:02     ` Paul Eggert
2023-02-12 10:21       ` Arsen Arsenović via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2023-02-13 20:47         ` Paul Eggert
2011-11-03 20:32   ` Lars Magne Ingebrigtsen
2011-11-04  9:36     ` Juri Linkov
2011-11-04 10:54       ` Eli Zaretskii
2022-02-07  0:10 ` Lars Ingebrigtsen
2022-02-07 19:41   ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=861qmvcglp.fsf@aarsen.me \
    --to=bug-gnu-emacs@gnu.org \
    --cc=9800@debbugs.gnu.org \
    --cc=arsen@aarsen.me \
    --cc=eggert@cs.ucla.edu \
    --cc=rms@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).