From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Arsen =?UTF-8?Q?Arsenovi=C4=87?= via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#9800: Incomplete truncated file buffers from the /proc filesystem Date: Sun, 12 Feb 2023 11:21:29 +0100 Message-ID: <861qmvcglp.fsf@aarsen.me> References: <877h40vb8h.fsf@mail.jurta.org> <4EA4D31B.4050604@cs.ucla.edu> <4EA5E08D.8070903@cs.ucla.edu> Reply-To: Arsen =?UTF-8?Q?Arsenovi=C4=87?= Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26391"; mail-complaints-to="usenet@ciao.gmane.io" Cc: rms@gnu.org, 9800@debbugs.gnu.org To: Paul Eggert Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Feb 12 11:40:16 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pR9mK-0006Vd-K9 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 12 Feb 2023 11:40:16 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pR9mB-0003Ro-Sp; Sun, 12 Feb 2023 05:40:07 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pR9m7-0003RA-DX for bug-gnu-emacs@gnu.org; Sun, 12 Feb 2023 05:40:05 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pR9m6-0000zH-RK for bug-gnu-emacs@gnu.org; Sun, 12 Feb 2023 05:40:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pR9m6-0004zQ-FW for bug-gnu-emacs@gnu.org; Sun, 12 Feb 2023 05:40:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Arsen =?UTF-8?Q?Arsenovi=C4=87?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 12 Feb 2023 10:40:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 9800 X-GNU-PR-Package: emacs Original-Received: via spool by 9800-submit@debbugs.gnu.org id=B9800.167619840119169 (code B ref 9800); Sun, 12 Feb 2023 10:40:02 +0000 Original-Received: (at 9800) by debbugs.gnu.org; 12 Feb 2023 10:40:01 +0000 Original-Received: from localhost ([127.0.0.1]:44131 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pR9m4-0004z6-TQ for submit@debbugs.gnu.org; Sun, 12 Feb 2023 05:40:01 -0500 Original-Received: from mout-p-201.mailbox.org ([80.241.56.171]:36662) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pR9m1-0004yr-J2 for 9800@debbugs.gnu.org; Sun, 12 Feb 2023 05:39:59 -0500 Original-Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4PF3rP2swhz9sTd; Sun, 12 Feb 2023 11:39:49 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aarsen.me; s=MBO0001; t=1676198389; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=W8Mzns93aeLsy3k2TsolnhEJEPrN88/nZZBzAYIQuuk=; b=HAQDWAbozNU8UVQ2OyIWoenqHnw863zxidMpmlxZSY3CdWGtcHQQ173z819xDawGUBxFWl SbIoSiBxHeM7cMRaPLdxMDalZRvrLnwq7o5lb5PLWd4SRDFIduFpcJ1vkG0daMk8kpuAaT kfT3KzfAotGi54jZizLPPUlXlMODG+/hay7+6ObymyLMc3fNU0i4YqTC1qvmkvjLaF6avw NTFizOMW4uHdOQxQxE6uaTJ5M44kWOUB2puzLTmhA5XmI7nmAbI4ZUtDVJxbByPLJySoqS snLPqz8wRVi+m87TABX1/1fKDDIq/wpx7IrKpBy29UdK2Ti0/ZuGM+8Q35P6hA== In-reply-to: <4EA5E08D.8070903@cs.ucla.edu> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:255395 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 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, thoug= h, > 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 =3D max (READ_BUF_SIZE, st.st_size) buf =3D malloc (bufsz) int ret =3D 0, readsz =3D 0; do { readsz +=3D ret; if (readsz =3D=3D bufsz && size isn't unreasonable) { /* value chosen arbitrarily. */ bufsz +=3D min (16 * READ_BUF_SIZE, bufsz) buf =3D realloc (buf, bufsz) } errno =3D 0 ret =3D read (fd, buf + readsz, bufsz - readsz) } while (ret > 0 || errno =3D=3D 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. =2D-=20 Arsen Arsenovi=C4=87 --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iOYEARYKAI4WIQT+4rPRE/wAoxYtYGFSwpQwHqLEkwUCY+jB818UgAAAAAAuAChp c3N1ZXItZnByQG5vdGF0aW9ucy5vcGVucGdwLmZpZnRoaG9yc2VtYW4ubmV0RkVF MkIzRDExM0ZDMDBBMzE2MkQ2MDYxNTJDMjk0MzAxRUEyQzQ5MxAcYXJzZW5AYWFy c2VuLm1lAAoJEFLClDAeosSTpsgA/AzGKjpqmFO5/JVaJ4lHgX/8eu3UoA4nuSzp hfmfrlMJAP9vR/K32mb45XCxXRza7h9lGxf8VUqdo5Mhb4wLm1WuCQ== =SRwa -----END PGP SIGNATURE----- --=-=-=--